如何重复程序的一部分直到输入正确? [英] How to repeat a section of the program until the input is correct?
本文介绍了如何重复程序的一部分直到输入正确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望重复我的代码,直到玩家猜对为止.
I want my code to be repeated until the player guesses correctly.
ghuess=input("state a number between 1-100")
if ghuess>number:
print "too high try again!"
elif ghuess<number:
print "too low try again!"
else:
print "well done! ghuess you have won.."
time.sleep(1)
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print prize
推荐答案
添加 <代码> while -在那里循环.这意味着您将无限次地循环问题,直到获得满意的结果为止.
Add a while
-loop there. This means you're looping the question again infinitely until you've reached a satisfactory result.
while True:
ghuess=input("state a number between 1-100")
if ghuess>number:
print "too high try again!"
elif ghuess<number:
print "too low try again!"
else:
# Jackpot, exit the loop.
break
print "well done! ghuess you have won.."
time.sleep(1)
print "3"
time.sleep(1)
print "2"
time.sleep(1)
print "1"
time.sleep(1)
print prize
这篇关于如何重复程序的一部分直到输入正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文