我在一个基本的编程课,我有点卡住这个游戏工作。这个想法是创建一个简单的猜字游戏,计算机会选择一个随机的单词,然后尝试先猜测单词中的某个字母,然后尝试5次尝试后再单词。我经历了多次,当我尝试运行模块时,仍然出现“无效语法”的错误。 当谈到编程语言时,我有点诵读困难,所以也许我忽略了一些东西。 如果有人可以提供一些帮助,我将不胜感激!用Python猜字游戏?
#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")
还应当指出的是,当我试图指定“尝试”变量的范围发生的问题“而尝试”块,结束后。我之前在随机数字游戏中使用过它,但由于某种原因,它在这里无法正常工作。我将非常感谢一些帮助! 还应该注意的是,我相信,我运行在一个相当过时的Python版本上,它的一些变体是2.0。
你期望什么'对于猜字:'做什么? – njzk2 2014-09-25 20:19:23
@ Hodge-PodgeCrush,我清理了OP中的空白。确保带'#*'的行在问题中具有与代码中相同的缩进。其他一切似乎都很好。 – jedwards 2014-09-25 20:22:51