完全正确的while循环语法无效?

时间:2011-12-11 00:04:36

标签: python

我正在编写一个有趣的程序(挂起男人),我在循环中获得了错误的语法

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 个答案:

答案 0 :(得分:4)

wrong MAX_WORD语法不正确。也许你想要wrong <= Max_Wrong

答案 1 :(得分:2)

wrong MAX_WRONG没有意义。您需要在这两个标识符之间使用运算符。您可能正在寻找<

答案 2 :(得分:1)

while wrong < MAX_WRONG and so_far != word:

这是你的意思吗?(注意<比较器)

答案 3 :(得分:1)

我认为你的意思是wrong < MAX_WRONG ...