带有两个输入调用的While循环

2024-09-25 00:32:33 发布

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

我正在学习Python,在while循环方面遇到了问题。下面有一个示例代码,它包含一个while循环。我要做的是打印“哈!你永远不会猜到“每次正确的名字没有被预测,但我也希望代码打印”不!你怎么猜到的?“当预测到正确的名字时。我一直在玩它,它做我想做的,但我必须测试我的代码系统,它说这是不正确的,我的程序试图读取更多的输入比它应该需要的。任何帮助都将不胜感激!你知道吗

print("You will never win the game, for Scooby dooby doo! is my name.")
rumple = input("Guess my name: ")

while rumple != "Rumplestiltskin":

        print("Ha! You'll never guess!")
        rumple = input("Guess my name: ")



print('No! How did you guess?')

Tags: 代码name程序you示例inputmy系统
2条回答

假设任务只有一个input函数,您可以将while循环改为

while "Rumplestiltskin" != input("Guess my name: "):
    print("Ha! You'll never guess!")

print('No! How did you guess?')

这会将input函数放入while循环,而不需要事先初始化rumple变量。你知道吗

你用哪种系统?我测试了你的代码,在我更正了缩进之后,我没有得到任何错误或警告。你知道吗

print("You will never win the game, for Scooby dooby doo! is my name.")
rumple = input("Guess my name: ")

while rumple != "Rumplestiltskin":
    print("Ha! You'll never guess!")
    rumple = input("Guess my name: ")

print('No! How did you guess?')

我的结果:

You will never win the game, for Scooby dooby doo! is my name.
Guess my name: Scooby
Ha! You'll never guess!
Guess my name: Rumplestiltskin
No! How did you guess?

Process finished with exit code 0

相关问题 更多 >