1

我目前正在学习 Python,我正在努力让这个游戏正常工作。基本上,我分配了一个要猜测的单词,然后将该单词切片并将其分配给其他几个变量。基本上,分配为“letterx”的每个变量都是组成字符串变量单词的一部分的字母。问题是让带有嵌套 if 语句的 while 语句起作用。出于某种原因,我无法得到等于 letterx 的猜测输入。当我运行代码时,我得到的只是“否”。然后是左转的数量。但是,我无法让 elif 语句起作用。几乎所有其他工作。我习惯于用 Java 编程,而且我对 Python 还很陌生,所以任何提示或帮助都将不胜感激。感谢您的时间和帮助!这是代码:

#Guess The Word

word = "action"

letter1 = ""
letter2 = ""
letter3 = ""
letter4 = ""
letter5 = ""
letter6 = ""

position1 = 0
position2 = 1
position3 = 2
position4 = 3
position5 = 4
position6 = 5

letter1 += word[position1]
letter2 += word[position2]
letter3 += word[position3]
letter4 += word[position4]
letter5 += word[position5]
letter6 += word[position6]

print("Welcome to Guess the Word!\n")


count = 6
while(count != 0):
    guess = input("Take a guess: \n")
    if(guess != letter1 or guess != letter2 or guess != letter3 or guess !=
       letter4 or guess != letter5 or guess != letter6):
        count -= 1
        print("No.\n")
        print("Turns left: \n", count)

    elif(guess == letter1 or guess == letter2 or guess == letter3
          or guess == letter4 or guess == letter5 or guess == letter6):
        count -= 1
        print("Yes.\n")


if(count == 0):
    print("Your turns are up, what do you think the word is?")
    guess = input("The word is...: \n")
if(guess == word):
    print("You win! That's the word")
elif(guess != word):
    print("Sorry, you lose.")

这是在 Python shell 中运行的程序:

Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Welcome to Guess the Word!

Take a guess: 
a
No.

Turns left: 
 5
Take a guess: 
c
No.

Turns left: 
 4
Take a guess: 
t
No.

Turns left: 
 3
Take a guess: 
i
No.

Turns left: 
2
Take a guess: 
o
No.

Turns left: 
 1
Take a guess: 
n
No.

Turns left: 
 0
Your turns are up, what do you think the word is?
The word is...: 
action
You win! That's the word
4

1 回答 1

0

假设guess等于letter1。那么尽管如此 guess == letter1,第一个条件仍然是 True since guess != letter2。同样,无论是什么guess,都有一些不相等的字母(在letter1,等之间)。letter2

所以第一个if条件总是 True。


相反,您可以使用

while(count != 0):
    guess = input("Take a guess: \n")
    if not guess in word:
        count -= 1
        print("No.\nTurns left: \n", count)
    else:
        count -= 1
        print("Yes.\n")

顺便说一句,应该完全可以在不定义letter1,letter2等的情况下编写游戏代码。所有这些代码都应该删除:

letter1 = ""
letter2 = ""
letter3 = ""
letter4 = ""
letter5 = ""
letter6 = ""

position1 = 0
position2 = 1
position3 = 2
position4 = 3
position5 = 4
position6 = 5

letter1 += word[position1]
letter2 += word[position2]
letter3 += word[position3]
letter4 += word[position4]
letter5 += word[position5]
letter6 += word[position6]

只需使用word[0]代替letter1word[1]代替letter2等。

请注意,您甚至可能不需要word[0], word[1]。例如,使用 Python,您可以使用

guess in word

代替

guess in (word[0], word[1], word[2], word[3], word[4], word[5])

它不仅打字少了很多,而且更通用,因为guess in word用任何长度的单词都可以做正确的事情。

于 2013-07-08T21:32:39.027 回答