如何重复一段程序,直到在python中输入正确为止? [英] How to repeat a section of program until the input is correct in python?
本文介绍了如何重复一段程序,直到在python中输入正确为止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望重复我的路线,直到玩家猜对为止
i want my cade 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
推荐答案
添加
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
这篇关于如何重复一段程序,直到在python中输入正确为止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文