Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While loops for a Python noob

Tags:

python

I'm trying to teach my son how to program & I gave him a challenge that I can't do myself.

He has to prompt the user to enter A, B, or C. Depending on their choice, he'll print some results & other stuff. While we got this working, I'm trying to do some error handling & check for cases where the input is not A, B, or C. The code is listed below. I certainly appreciate any guidance you can offer.

print "************************************************************"
print "*                                                          *"
print "*            Welcome to the cinemas                        *"
print "*                                                          *"
print "************************************************************"
print "*                                                          *"
print "*  What movie would you like to see ?                      *"
print "*                                                          *"
print "*  A) Star Wars                                            *"
print "*  B) Bourne Identity                                      *"
print "*  C) The Hunger Games                                     *"
print "*                                                          *"
print "************************************************************"
movie=raw_input()
while(movie!="A","B","C"):
    print "************************************************************"
    print "*                                                          *"
    print "*            Welcome to the cinemas                        *"
    print "*                                                          *"
    print "************************************************************"
    print "*                                                          *"
    print "*  What movie would you like to see ?                      *"
    print "*                                                          *"
    print "*  A) Star Wars                                            *"
    print "*  B) Bourne Identity                                      *"
    print "*  C) The Hunger Games                                     *"
    print "*                                                          *"
    print "************************************************************"
    movie=raw_input()

print "************************************************************"
print "*                                                          *"
print "*  How many tickets would you like ?                       *"
print "*                                                          *"
print "************************************************************"
quantity =input()
cost = 7.5
if movie =="A":
    print "You are seeing Star Wars"
    price = cost*quantity
    print "You owe ", price
elif movie =="B":
    print "You are seeing Bourne Identity"
    price = cost*quantity
    print "You owe ", price
elif movie =="C":
    print "You are seeing The Hunger Games"
    price = cost*quantity
    print "You owe ", price 
like image 696
Marc St. Onge Avatar asked Jan 15 '23 10:01

Marc St. Onge


2 Answers

You want to do while movie not in ("A", "B", "C").

movie != "A", "B", "C" checks whether movie is equal to the three-element tuple ("A", "B", "C"), which it never will be.

like image 78
BrenBarn Avatar answered Jan 16 '23 23:01

BrenBarn


Here are a couple of better ways to structure your loop to avoid repetition.

First way is to set the movie to an invalid value, this means the loop will always execute at least once

movie = None
while movie not in ("A", "B", "C"):
    print "************************************************************"
    print "*                                                          *"
    print "*            Welcome to the cinemas                        *"
    print "*                                                          *"
    print "************************************************************"
    print "*                                                          *"
    print "*  What movie would you like to see ?                      *"
    print "*                                                          *"
    print "*  A) Star Wars                                            *"
    print "*  B) Bourne Identity                                      *"
    print "*  C) The Hunger Games                                     *"
    print "*                                                          *"
    print "************************************************************"
    movie = raw_input()

The second way is to use a while True: loop

while True:
    print "************************************************************"
    print "*                                                          *"
    print "*            Welcome to the cinemas                        *"
    print "*                                                          *"
    print "************************************************************"
    print "*                                                          *"
    print "*  What movie would you like to see ?                      *"
    print "*                                                          *"
    print "*  A) Star Wars                                            *"
    print "*  B) Bourne Identity                                      *"
    print "*  C) The Hunger Games                                     *"
    print "*                                                          *"
    print "************************************************************"
    movie = raw_input()
    if movie in ("A", "B", "C"):
        break

Then you can work on storing the movies in a variable as Alex suggests

like image 29
John La Rooy Avatar answered Jan 16 '23 22:01

John La Rooy