Python Forum
stock in while loop can someone help?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
stock in while loop can someone help?
#1
hi im lerninig this cheapter about while loop , and trying to get out from the loop ,
the excrice was to build a tikeat progrem , this what i have done .
active = True
while active:
	message = input("what age  you are")
	ticket = int(message)
	if ticket <= 4:
		print("you can enter freely becuse your samell")
	elif ticket <=13:
		print("the ticket will be for you 10$")
	elif ticket >= 12:
		print("the ticket will be for you 15$")
	elif ticket == "quit":
		active = False
and when im trying to wuit the program it writing the next erorer

Error:
Traceback (most recent call last): File "C:\Users\alex8\Documents\python\python _work.py\Chpter 6\moive.py", line 5, in <module> if ticket <= 4: TypeError: '<=' not supported between instances of 'str' and 'int'
thanks for the help
Reply
#2
Your code includes conversion from message to int, so the error message you provided is no longer applicable. However the conversion causes another problem because the user cannot enter "quit" because that is not something that can be turned into an int.

You can test for quit before doing the conversion:
while True:
    message = input("what age you are")
    if message == "quit":
        break;
    ticket = int(message)
    if ticket <= 4:
        print("you can enter freely becuse your samell")
    elif ticket <= 13:
        print("the ticket will be for you 10$")
    else:
        print("the ticket will be for you 15$")
This is better, but still a very weak test. What if the user enters something that is not "quit" and isn't a number. What happens then?
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020