2014-09-25 127 views
-1

我在一个基本的编程课,我有点卡住这个游戏工作。这个想法是创建一个简单的猜字游戏,计算机会选择一个随机的单词,然后尝试先猜测单词中的某个字母,然后尝试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。

+0

你期望什么'对于猜字:'做什么? – njzk2 2014-09-25 20:19:23

+0

@ Hodge-PodgeCrush,我清理了OP中的空白。确保带'#*'的行在问题中具有与代码中相同的缩进。其他一切似乎都很好。 – jedwards 2014-09-25 20:22:51

回答

1
tries = tries + 1 

    if tries == 5: 
     final = raw_input ("Try to guess the word!: ") 

     if final == correct: 

你需要==比较不==是用于分配。

你也可以用tries += 1

更换tries = tries + 1您还想要移动raw_input内环路,只是要求用户猜测,而在外面的话,当猜测都用完:

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!" 
0

请试试这个。

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 *************")