Python中的猜字游戏?
我上的是一门基础编程课,我一直在做这个游戏。这个想法是创建一个简单的单词猜测游戏,在这个游戏中,计算机会随机选择一个单词,你会尝试先猜测单词中的一些字母,然后在5次尝试后猜测单词本身。我已经经历了多次,但当我尝试运行模块时,仍然会出现“无效语法”错误。 说到编程语言,我有点诵读困难,所以也许我忽略了什么? 如果有人能提供一些帮助,我将不胜感激Python中的猜字游戏?,python,Python,我上的是一门基础编程课,我一直在做这个游戏。这个想法是创建一个简单的单词猜测游戏,在这个游戏中,计算机会随机选择一个单词,你会尝试先猜测单词中的一些字母,然后在5次尝试后猜测单词本身。我已经经历了多次,但当我尝试运行模块时,仍然会出现“无效语法”错误。 说到编程语言,我有点诵读困难,所以也许我忽略了什么? 如果有人能提供一些帮助,我将不胜感激 #Word Guessing Game #Computer picks a random word #Player tries to guess it #
#Word Guessing Game
#Computer picks a random word
#Player tries to guess it
#computer only responds with yes or no
import random
tries = 0
print "Welcome to the word game!"
print "\nI'm going to think of a word and you have to guess it!"
print "\nGuess which letters are in the word, then you have to guess the whole thing!"
print "\nGood luck!"
WORDS = ("follow", "waking", "insane", "chilly", "massive",
"ancient", "zebra", "logical", "never", "nice")
word = random.choice(WORDS)
correct = word
length = len(word)
length = str(length)
guess = raw_input("The word is " + length + " letters long. Guess a letter!: ")
while tries < 5:
for guess in word:
if guess not in word:
print "Sorry, try again."
else:
print "Good job! Guess another!"
tries = tries + 1 #*
if tries = 5:
final = raw_input ("Try to guess the word!: ")
if final = correct:
print "Amazing! My word was ", word, "!"
else:
print "Sorry. My word was ", word, ". Better luck next time!"
raw_input("\n\nPress enter to exit")
猜单词游戏
#计算机随机挑选一个单词
#玩家试图猜测它
#计算机只响应是或否
随机输入
尝试=0
打印“欢迎来到文字游戏!”
打印“\n我想一个词,你必须猜出来!”
打印“\n根据单词中的字母,您必须猜出整个单词!”
打印“\n祝你好运!”
单词=(“跟随”,“清醒”,“疯狂”,“寒冷”,“巨大”,
“古代”、“斑马”、“逻辑”、“从未”、“美好”)
单词=随机。选择(单词)
正确的
长度=长度(字)
长度=str(长度)
猜=原始输入(“单词是“+长度+”字母长。猜一个字母!:”)
当尝试<5时:
对于单词猜测:
如果猜测不在文字中:
打印“对不起,再试一次。”
其他:
打印“干得好!猜另一个!”
尝试=尝试+1#*
如果尝试次数=5:
final=原始输入(“试着猜单词:”)
如果final=正确:
打印“太棒了!我的单词是”,单词“!”
其他:
打印“对不起,我的话是”,字,“祝你下次好运!”
原始输入(“\n\n按enter键退出”)
还应注意的是,问题发生在“while trys”块结束后,当时我试图指定“trys”变量的限制。我以前在一个随机数游戏中使用过它,但由于某些原因,它在这里不能正常工作。我将非常感谢您的帮助!
还应该注意的是,我运行的是一个相当过时的Python版本,我相信是2.0的一些变体
tries = tries + 1
if tries == 5:
final = raw_input ("Try to guess the word!: ")
if final == correct:
您需要=
进行比较,而不是=
,=
用于分配
您还可以将tries=tries+1
替换为tries+=1
您还希望将raw_input
移动到循环内部,并在猜测完后,让用户在while之外猜测单词:
while tries < 5:
guess = raw_input("The word is " + length + " letters long. Guess a letter!: ")
if guess not in word: # use `in` to check if the guess/letter is in the word
print "Sorry, try again."
else:
print "Good job! Guess another!"
tries += 1
# guesses are all used up when we get here
final = raw_input ("Try to guess the word!: ")
if final == correct:
print "Amazing! My word was ", word, "!"
else:
print "Sorry. My word was ", word, ". Better luck next time!"
尝试<5次时:
猜=原始输入(“单词是“+长度+”字母长。猜一个字母!:”)
如果猜测不在单词中:#使用'in'检查猜测/字母是否在单词中
打印“对不起,再试一次。”
其他:
打印“干得好!猜另一个!”
尝试次数+=1
#我们到这里时,猜测都用光了
final=原始输入(“试着猜单词:”)
如果final==正确:
打印“太棒了!我的单词是”,单词“!”
其他:
打印“对不起,我的话是”,字,“祝你下次好运!”
请试试这个
import random
index = -1
infi = ("python","jumble","hide","mama")
word = random.choice(infi)
word_len = len(word)
guess = ""
attempt = 0
enter = ""
print(word)
temp = "_" * word_len
print("\t\t The word chosen by computer contain", word_len,"letters.")
print("\t Tap any letter to check if this letter is in computer word.\n")
print("\t You got 5 attempts to check if tapped letter is in computer word.\n")
print("\t\t\t\t GOOD LUCK!!!\n\n\n\n")
for i in range(0, 5):
attempt +=1
guess = input("Attempt no. "+str(attempt)+":")
if guess in word and guess != enter:
for i in range(0, word_len):
if guess == word[i]:
temp = temp[:i] + guess +temp[i+1:]
print("yes\n" + temp)
if guess not in word:
print("no")
if "_" not in temp:
print("\t\t*********** Congratulation!! You guess the word *************")
break
elif attempt == 5:
guess = input("And the word is:")
if guess == word:
print("\t\t*********** Congratulation!! You guess the word *************")
else:
print("\t\t*********** WRONG!! Shame on you *************")
对于单词中的猜测:你期望
做什么?@Hodge PodgeCrush,我清理了OP中的空白。确保带有.*
的行在问题中的缩进与代码中的缩进相同。其他一切似乎都很好。