Vì vậy, tôi về cơ bản đang làm việc trên một dự án nơi máy tính lấy một từ trong danh sách các từ và làm cho nó rung lên cho người dùng. chỉ có một vấn đề: tôi không muốn phải viết hàng tấn từ trong danh sách, vì vậy tôi tự hỏi liệu có cách nào nhập một tấn từ ngẫu nhiên cho nên thậm chí tôi không biết nó là gì, và sau đó tôi có thể thưởng thức các trò chơi quá? Đây là mã hóa của toàn bộ chương trình, nó chỉ có 6 từ mà tôi đặt trong:Trình tạo từ ngẫu nhiên- Python
import random
WORDS = ("python", "jumble", "easy", "difficult", "answer", "xylophone")
word = random.choice(WORDS)
correct = word
jumble = ""
while word:
position = random.randrange(len(word))
jumble += word[position]
word = word[:position] + word[(position + 1):]
print(
"""
Welcome to WORD JUMBLE!!!
Unscramble the leters to make a word.
(press the enter key at prompt to quit)
"""
)
print("The jumble is:", jumble)
guess = input("Your guess: ")
while guess != correct and guess != "":
print("Sorry, that's not it")
guess = input("Your guess: ")
if guess == correct:
print("That's it, you guessed it!\n")
print("Thanks for playing")
input("\n\nPress the enter key to exit")
Sử dụng một file văn bản của từ? '/ usr/share/dict/words' là các nền tảng phổ biến * nix, hoặc có các danh sách từ khác mà bạn có thể sử dụng ... –
có thể trùng lặp của [Danh sách từ miễn phí để sử dụng theo chương trình?] (http://stackoverflow.com/questions/ 772922/free-word-list-for-use-programatically) – Bakuriu