pythonisawesome
pythonisawesome

Reputation: 11

How can I make if-statements recurring until the user types 'quit'?

I am coding in Python.

Let us say when the program starts, the user is prompted with the following:

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.

Whatever the user types in, the if statement is executed. I want this program to keep going until the user types quit. Nonetheless, as a user, I want to be able to keep hitting 1) or 2) as many times as I want.

Here is my code thus far:

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

The only thing that I want to make this program stop is if the user types "quit."

How would I go about doing this? Do I need a while loop comprised of if statements? If so, how would I go about doing that?

Upvotes: 0

Views: 125

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1121992

Just put everything in a while True loop, then use break when the user entered quit:

while True:
    x = raw_input("Enter number here (or 'quit' to end): ")
    if x == 'quit':
        break

    # ...

Upvotes: 5

Related Questions