我在Python编码。
让我们说一下程序启动时,系统会提示用户:
Press 1) to check what the temperature is outside.
Press 2) to find out how many miles are on your car.
Press 3) to see how full your gas tank is.
Press 4) to find out how much money you made last week.
无论用户输入什么,都会执行if语句。我希望这个程序继续运行,直到用户输入quit。尽管如此,作为一个用户,我希望能够按照我想要的次数继续击中1)或2)。
到目前为止,这是我的代码:
x = raw_input("Enter number here: ")
if x == '1':
weather = 46
print "%s degrees" % (weather)
if x == '2':
milesoncar = '30,000'
print "%s miles" % (milesoncar)
if x == '3':
gasintank = 247.65
print "%s miles left in gas tank" % (gasintank)
if x == '4':
money = '$439'
print "You made %s last week." % (money)
if x == 'quit':
print 'Goodbye
我想让这个程序停止的唯一一件事是用户输入"退出。"
我该怎么做呢?我需要一个由if语句组成的while循环吗?如果是这样,我将如何做到这一点?
答案 0 :(得分:5)
只需将所有内容放入while True
循环中,然后在用户输入break
时使用quit
:
while True:
x = raw_input("Enter number here (or 'quit' to end): ")
if x == 'quit':
break
# ...