While循环的问题(Python 2.7)
我的循环有点问题。这是我的一个项目: “编写一个程序,提示用户猜测你的名字 应继续询问,直到用户猜对了名称。猜对后,向用户发送祝贺消息 用户被告知他/她只猜了十次。当用户正确猜到名称或猜错了十次时,循环应该退出 问题是,当我在while循环中时,如果我输入“Name”,它仍然会记录我是错误的 以下是我目前拥有的:While循环的问题(Python 2.7),python,python-2.7,while-loop,Python,Python 2.7,While Loop,我的循环有点问题。这是我的一个项目: “编写一个程序,提示用户猜测你的名字 应继续询问,直到用户猜对了名称。猜对后,向用户发送祝贺消息 用户被告知他/她只猜了十次。当用户正确猜到名称或猜错了十次时,循环应该退出 问题是,当我在while循环中时,如果我输入“Name”,它仍然会记录我是错误的 以下是我目前拥有的: count = 0 print "Hello. I have a name inside my head. Please guess what name it is." text =
count = 0
print "Hello. I have a name inside my head. Please guess what name it is."
text = raw_input ("What is your answer? ")
while (text != "Name"):
count = count + 1
attempt = 10 - count
if count >= 10:
print "That is the maximum amount of attempts allowed. Sorry!"
stopper = raw_input ("Thanks for playing!")
if count < 10:
print "Sorry, that is not the answer. You have", attempt, "more attempts."
newText = raw_input ("What is your next guess? ")
if (text == "Name"):
print "Amazing. You got it correct!"
count=0
打印“你好。我脑子里有一个名字。请猜猜它叫什么名字。”
text=原始输入(“您的答案是什么?”)
而(文本!=“名称”):
计数=计数+1
尝试=10-计数
如果计数>=10:
打印“这是允许的最大尝试次数。很抱歉"
stopper=原始输入(“感谢您的播放!”)
如果计数小于10:
打印“对不起,这不是答案。你有更多的尝试
newText=原始输入(“您下一步的猜测是什么?”)
如果(文本=“名称”):
打印“惊人”。你说得对!"
这是因为您在while循环之外设置的变量文本上进行迭代和测试,这意味着它将只捕获用户的第一个输入,这是唯一成功的例子。
给你:
count = 0
print("Hello. I have a name inside my head. Please guess what name it is.")
text = input("What is your answer? ")
while (count<12):
count = count + 1
attempt = 10 - count
if (text == 'Name'):
print("Amazing. You got it correct!")
count = 12
if count >= 10:
print("That is the maximum amount of attempts allowed. Sorry!")
stopper = input ("Thanks for playing!")
if count < 10:
print ("Sorry, that is not the answer. You have", attempt, "more attempts.")
text = input ("What is your next guess? ")
count=0
打印(“你好,我脑子里有个名字,请猜猜是什么名字。”)
text=输入(“您的答案是什么?”)
而(计数=10:
打印(“这是允许的最大尝试次数。对不起!”)
stopper=输入(“感谢您的参与!”)
如果计数小于10:
打印(“对不起,这不是答案。您有”,尝试,“更多尝试”。)
text=输入(“您下一步的猜测是什么?”)
您需要对代码进行一些更改:
(1) 用text
替换new\u text
,以便在每次迭代中更新text
的值
(2) 一旦用户达到最大尝试次数,就中断while
循环
(3) 当计数达到最大值时,要求用户输入文本是没有意义的,因此与其获得raw\u input
,不如打印“感谢您的播放!”
以下是有效的更新代码:
count = 0
print "Hello. I have a name inside my head. Please guess what name it is."
text = raw_input ("What is your answer? ")
while (text != "Name" and count < 10):
count = count + 1
attempt = 10 - count
if count >= 10:
print "That is the maximum amount of attempts allowed. Sorry!"
print "Thanks for playing!"
if count < 10:
print "Sorry, that is not the answer. You have", attempt, "more attempts."
text = raw_input ("What is your next guess? ")
if (text == "Name"):
print "Amazing. You got it correct!"
count=0
打印“你好,我脑子里有个名字,请猜猜是什么名字。”
text=原始输入(“您的答案是什么?”)
而(文本!=“名称”和计数<10):
计数=计数+1
尝试=10-计数
如果计数>=10:
打印“这是允许的最大尝试次数。抱歉!”
打印“谢谢玩!”
如果计数小于10:
打印“对不起,这不是答案。您有”,尝试,“更多尝试。”
text=原始输入(“您下一步的猜测是什么?”)
如果(文本=“名称”):
打印“太棒了,你说对了!”
你只分配给文本
一次。你是什么意思?我应该在哪里删除变量,文本?如果文本
从未改变,你希望如何结束循环?哦,是的。谢谢!谢谢你的建议。但是,我的老师不允许中断,因为她说这是“作弊”"这是一个漏洞。不过,谢谢你的建议。我知道我哪里搞砸了。我真是个傻瓜。XD如果我没有原始输入,它只是垃圾邮件打印命令。没有中断或原始输入,有没有其他方法来阻止垃圾邮件?如果你不想使用中断
,你可以在while循环中添加计数<10
条件。我已经更新了我的代码来解决这个问题。