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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With