Python Forum
stock in while loop can someone help? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Homework (https://python-forum.io/forum-9.html)
+--- Thread: stock in while loop can someone help? (/thread-26835.html)



stock in while loop can someone help? - yukhei - May-15-2020

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


RE: stock in while loop can someone help? - deanhystad - May-15-2020

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?