为什么我的代码会在第二个提示之后跳过输入提示?

2024-05-23 21:33:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经看了一段时间了,非常感谢您的帮助

secret = "Meow"
guess = ""
limit = 1

while guess != secret and limit <= 5:
    if limit == 2:
        print("You have 4 guesses left!")
    elif limit == 3:
        print("You have 3 guesses left!")
    elif limit == 4:
        print("You have 2 guesses left!")
    elif limit == 5:
        print("You have 1 guess left!")

    if limit == 1:
        guess = input("What's the secret? OwO You have 5 guesses. ")
    elif limit <= 2:
        guess = input("What's the secret? OwO. ")

    limit += 1

print("You cheated! Or lost.")

当运行时,它通常会进行输入尝试,在答错问题后,它会说还剩5次尝试并再次询问输入,但之后它会跳过输入提示,只说还剩猜测。带猜测的输出:

What's the secret? OwO You have 5 guesses. idk man  
You have 4 guesses left!  
What's the secret? OwO. 2nd try?  
You have 3 guesses left!  
You have 2 guesses left!  
You have 1 guess left!  
You cheated! Or lost.  

Tags: theyouinputsecretifhaveleftwhat
2条回答

只要改变一下:

elif limit >= 2:

secret = "Meow"
guess = ""
limit = 1

while guess != secret and limit <= 5:
    if limit == 2:
        print("You have 4 guesses left!")
    elif limit == 3:
        print("You have 3 guesses left!")
    elif limit == 4:
        print("You have 2 guesses left!")
    elif limit == 5:
        print("You have 1 guess left!")

    if limit == 1:
        guess = input("What's the secret? OwO You have 5 guesses. ")
    elif limit >= 2:
        guess = input("What's the secret? OwO. ")

    limit += 1

print("You cheated! Or lost.")

下面是对代码的改进

secret = "Meow"
guess = ""
limit = 1

while guess != secret and limit <= 5:

    if limit == 1:
        guess = input("What's the secret? OwO You have 5 guesses. ")
    elif limit >= 2: #here was your mistake
        print("You have {0} guess left!".format(5-limit+1))
        guess = input("What's the secret? OwO. ")

    limit += 1

print("You cheated! Or lost.")

相关问题 更多 >