完全正确的while循环却出现语法错误?

-8 投票
4 回答
600 浏览
提问于 2025-04-17 08:08

我正在写一个有趣的程序(猜单词游戏),但是在这个循环里出现了语法错误。

while wrong MAX_WRONG and so_far != word:

我的整个程序就是这个。

import random
HANGMAN=( #this is the Hangman ascii art
"""
_________
   |         |
   |         0
   |        
   |        
   |
   |
 _________
    |         |
    |         0
    |         |
    |        
    |
    |
 _________
    |         |
    |         0
    |        /|\\
    |        
    |
    |
 _________
    |         |
    |         0
    |        /|\\
    |        / \\
    |
    |
""")

MAX_WRONG=len(HANGMAN)-1


WORDS=("OVERUSED","CLAM","BACON","PUCK","TAFFY") #these are the words

word=random.choice(WORDS) #this is teh word that is going to be guessed
so_far="-"*len(word)#where the orrect letteres are viewd
wrong=0
used=[]# the letters incorrectly guessed

while wrong MAX_WRONG and so_far != word:
    print HANGMAN[wrong]
    print "YOu have used:\n",used
    print "\nso far the word is:\n",so_far

guess=raw_input("\n\nEnter your guess:")
guess=guess.upper()

while guess in used:
    print "You have already guessed the letter".guess
    guess=raw_input("enter your guess")
    guess=guess.upper()
    used.append(guess)
if guess in word:
    new=""
    for i in range(len(word)):
        if guess==word[i]:
            new+=guess
        else: 
            new+=so_far[i]
    so_far=new

else:
    print "INCORRECT"
    wrong+=1
if wrong==MAX_WRONG:
    print HANGMAN[wrong]
else:
    print "YAAAAY"

    print "the word was",word

非常感谢大家的帮助!

4 个回答

1
while wrong < MAX_WRONG and so_far != word:

这是不是你想表达的意思?(注意 < 这个比较符号)

4

wrong MAX_WORD 这个写法是错误的。也许你想写的是 wrong <= Max_Wrong

2

wrong MAX_WRONG 这个表达没有意义。你需要在这两个标识符之间加一个运算符。你可能是想用 <(小于号)。

撰写回答