为什么python中的变量不起作用

时间:2015-03-13 16:30:40

标签: python loops variables while-loop

我想创建一个游戏,计算机选择一个随机单词,玩家必须猜出这个单词。然后,计算机将告诉玩家该单词中有多少个字母。然后玩家有五次机会询问一个字母是否在单词中。计算机只能回答“是”或“否”。然后,玩家必须猜出这个词。

不知何故,我在下面写的程序并没有准确地让玩家有机会在让玩家猜出这个单词之前询问字母是否在单词中。我可以知道出了什么问题吗?谢谢!

    import random

    WORDS = ("hello", "running", "help", "united")
    word = random.choice(WORDS)

    correct = word
    letters=len(word)

    print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."

    guess_letter = raw_input("Guess a letter in the word.")
    tries = 0

    while guess_letter in word:
    tries +=1
    print "Yes"
    guess_letter = raw_input("Guess another letter in the word.")
    if tries == 4:
      print "Please guess the word."
    answer = raw_input("What is the word?")
    if answer == correct:
      print "That is correct!"
    else:
      print "You lose."

    while guess_letter not in word:
    tries +=1
    print "No"
    guess_letter = raw_input("Guess another letter in the word.")
    if tries == 4:
      print "Please guess the word."
    answer = raw_input("What is the word?")
    if answer == correct:
      print "That is correct!"
    else:
      print "You lose."

3 个答案:

答案 0 :(得分:1)

两个while循环一个接一个地是不正确的逻辑(报告它时的缩进也被打破了,但我猜测这只是你的副本的问题 - 和 - 粘贴到这里,而不是你的代码本身,否则你会得到语法错误: - )。

例如,假设玩家第一次在单词中选择guess_letter :然后第一个while立即退出并且永远不会再次进入

你需要一个单个循环,其迭代独立于单词中guess_letter是或不是 - 只有打印的内容应该取决于该检查! for循环可能更具可读性,但如果您愿意,可以使用while完美地执行此操作 - 但它可能类似于:

while tries < 5:
    tries += 1
    guess_letter = raw_input('Guess another letter in the word.')
    if guess_letter in word:
        print 'yes'
    else:
        print 'no'
print 'Please guess the word.'

用它之前的初始化和最后一次猜测并在它之后检查。

我也看到你想以不同方式对待第一次猜测(具体来说,使用不同的提示)。这可能是通过在提示符中使用变量来实现的最佳目的......:

tries = 0
art = 'a'
while tries < 5:
    tries += 1
    guess_letter = raw_input('Guess ' + art + ' letter in the word.')
    art = 'another'
    if guess_letter in word:
        print 'yes'
    else:
        print 'no'
print 'Please guess the word.'

答案 1 :(得分:0)

以下作品。它很草率,我不喜欢它,但我认为你最大的问题可能是缩进。之后我添加了一个while循环来嵌套你的另外两个循环。

如果你想改变一些东西来清理它我会建议一个while循环(尝试&lt; 4)然后用if else语句替换原来的while循环。

import random

WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)

correct = word
letters=len(word)

print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."

guess_letter = raw_input("Guess a letter in the word.")

while tries < 4:        
    while guess_letter in word:
        tries +=1
        print "Yes"
        guess_letter = raw_input("Guess another letter in the word.")
        if tries == 4:
            print "Please guess the word."
            answer = raw_input("What is the word?")
            if answer == correct:
                print "That is correct!"
            else:
                print "You lose."
        break
    while guess_letter not in word:
        tries +=1
        print "No"
        guess_letter = raw_input("Guess another letter in the word.")
        if tries == 4:
            print "Please guess the word."
            answer = raw_input("What is the word?")
            if answer == correct:
                print "That is correct!"
            else:
                print "You lose."
        break        

清理版本:

import random

WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)

correct = word
letters=len(word)
tries = 0

print "There are", len(word), "letters in the word. You have 5 chances to guess the letter in the word. After which you will be required to guess the word."

while tries < 4:
    tries +=1
    guess_letter = raw_input("Guess a letter in the word.")
    if guess_letter in word:
        print "Yes"

    else:
        print "No"

print "Please guess the word."
answer = raw_input("What is the word?")
if answer == correct:
    print "That is correct!"
else:
    print "You lose."

答案 2 :(得分:0)

您的代码似乎太复杂了。我建议你使用适合你问题的for循环。这是游戏的轻量级版本(在Python 3.4上测试):

import random

WORDS = ("hello", "running", "help", "united")
word = random.choice(WORDS)

print("There are {} letters in the word.".format(len(word)),
      "You have 5 chances to guess a letter in the word.",
      "After which you will be required to guess the word.")

for _ in range(5):
    guess_letter = input("Guess a letter in the word: ")
    if guess_letter in word:
        print("YES")
    else:
        print("NO")

answer = input("Guess the word: ")
if answer == word:
    print("That is correct!")
else:
    print("Game over!")