用Python猜词游戏?

时间:2014-09-25 20:16:42

标签: python

我在基础编程课程中,我在这个游戏上工作有点困难。这个想法是创建一个简单的猜词游戏,计算机会选择一个随机单词,你会尝试首先猜测单词中的一些字母,然后是5次尝试后的单词本身。我经历了多次,当我尝试运行模块时仍然出现“无效语法”错误。 在编程语言方面,我有点痴迷,所以也许我有些东西可以忽略? 如果有人可以提供一些帮助,我将不胜感激!

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

还应该注意,当我尝试指定“尝试”变量的限制时,在“while attempts”块结束后出现问题。我曾经在随机数游戏中使用它,但由于某种原因它在这里没有正常工作。我非常感谢一些帮助! 还应该注意的是,我运行的是一个相当过时的Python版本,我相信2.0的一些变体。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分: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 *************")