更正用户输入以满足Hangman的特定要求

时间:2014-10-20 16:46:20

标签: python loops

我需要用户输入只等于1个字符,但代码中有一个错误,允许我一次输入多个字符。我需要能够提示用户说你一次只能输入一个角色,然后回去让他们进入角色但没有角色指向游戏。

print ("lets play guess this word.") #time to play 

secret_word = input("Enter a secret word to guess: ") # gather the secret word
secret_word = secret_word.lower() # convert to lower 
guesses = "" # this is the string that will show what has been guessed later
turns = 6 # how many chances they get to enter a wrong character. 

# a very very tricky while loop. 
while turns > 0:         # will be true until the turns counter makes turns = 0
    count = 0   # this count determines wether to print _ or the character
    for char in secret_word: # looking at characters in the secret word
        if char in guesses: #  this is used to display the correct letters and _'s
            print (char, end="")   
        else:
            print ("_ ",end="") # this print the _ for every char in secretword
            count += 1   # ends the for loop continues to the next part 
    if count == 0: # you won the game end the loop. 
        print ()
        print ("You win")  
        break              
    print ()
    print ()
    print ()
    guess = input("guess a character:")
    count2 = 0
    if len(guess) > 1:
        count2 += 1
        while count2 > 0:
            print ("you can only guess 1 character at a time")
            count2 -= 1
    guess = guess.lower()  #lower        # it is time to guess the letters. 
    guesses += guess                  
    if guess not in secret_word:  # if statement for if guess is not in word
        turns -= 1       # subtract from turns
        print ()
        print ()
        print ("Wrong")   
        print ("Letters guessed so far: ",guesses) # end of loop show guessed letters
        print ("You have", + turns, 'more guesses') # show turns left
        if turns == 0:   # END GAME        
            print ("The word was",secret_word,"You Loose")  

显示代码在Python中工作的屏幕截图:

code working in Python

我还需要帮助,一次只接受1个字符,也没有数字。我还尝试完成此任务时添加了这部分代码,但它并没有阻止输入的多个字符从计数到字。

count2 = 0
    if len(guess) > 1:
        count2 += 1
        while count2 > 0:
            print ("you can only guess 1 character at a time")
            count2 -= 1

这是我的输出:

lets play guess this word.
Enter a secret word to guess: computer
_ _ _ _ _ _ _ _ 


guess a character:abcdefghijklmnopqrstuvwxyz
you can only guess 1 character at a time


Wrong
Letters guessed so far:  abcdefghijklmnopqrstuvwxyz
You have 5 more guesses
computer
You win

1 个答案:

答案 0 :(得分:1)

如果用户输入无效输入,则在警告后循环返回的语句。使用你得到的结构,你想要的语句是continue,它跳转到循环的下一个迭代:

while turns > 0:
    # Print current guesses; get input; etc...

    # Check for invalid input
    if len(guess) > 1:
        print("you can only guess 1 character at a time")
        continue  # This makes us return to the top of the while loop.

    # We definitely have valid input by the time we get here, so handle the new guess.

这与您的版本略有简化;例如,我已经取出count2,因为它没有做任何重要的事情。但前提是相同的:在你警告用户他们的无效输入后,你需要要求 new 输入 - 跳到循环的顶部 - 而不是仅仅向前移动。