关于循环:为什么python中的变量不起作用

Why did my variables in python not work

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

不知怎么的,我在下面写的程序并没有给玩家5个机会,让他们在猜单词之前先问字母是否在单词中。我能知道出了什么问题吗?谢谢您!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
    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."


两个while循环一个接一个是不正确的逻辑(您报告的缩进也被破坏了,但我猜这只是您复制和粘贴在这里的问题,而不是您的代码本身,否则您将得到语法错误:—)。

例如,假设玩家第一次选择一个不在单词中的guess_letter:那么第一个while会立即退出,不再进入!

您需要一个单独的循环,它的迭代与guess_letter是否在单词中无关——只有打印的内容应该依赖于该检查!for循环可能更易读,但如果您愿意的话,您可以很好地使用while,但它类似于:

1
2
3
4
5
6
7
8
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.'

在它之前进行初始化,在它之后进行最后的猜测和检查。

我还看到您希望以不同的方式对待第一个猜测(特别是使用不同的提示)。最好是在提示中使用一个变量来实现这一点……:

1
2
3
4
5
6
7
8
9
10
11
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.'


您的代码似乎太复杂了。我建议您使用一个适合您的问题的for循环。以下是游戏的轻量级版本(在Python3.4上测试):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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!")


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

如果你想改变一些东西来清理它,我建议使用while循环(while trys<4),然后用if else语句替换原来的while循环。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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

已清理版本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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."