Hello,
I am creating a code which if the input is numerical, certain text will be printed towards the user. If 'quit' is entered, the program stops. If a blank text, or any other string value at all is passed to it: I would like to print a message to advise the user to provide a relevant value.
However, I noticed that if I place a condition for 'else' at the end of the while loop, it will generate an error because the previous 'elif's are converting the value to an integer (if I understand it correctly) due to the previous numerical comparisons, resulting in an error.
If I place a condition of 'elif' before the numerical comparisons about rejecting any other value than 'quit', then the program will not run past that statement anymore since every value other than quit includes numerical values.
I have seen that 'try/except' might be a solution for this from reading other users questions about similar issues, but I haven't been able to do this correctly. I place the conditions for the 'if/else' in the 'else' portion of the 'try/except' block, then I still get the same issue. At that point, the value is storing integers which cause an error if a string value other than 'quit' is entered.
In any event, here is the code I am working with:
Thank you!
I am creating a code which if the input is numerical, certain text will be printed towards the user. If 'quit' is entered, the program stops. If a blank text, or any other string value at all is passed to it: I would like to print a message to advise the user to provide a relevant value.
However, I noticed that if I place a condition for 'else' at the end of the while loop, it will generate an error because the previous 'elif's are converting the value to an integer (if I understand it correctly) due to the previous numerical comparisons, resulting in an error.
If I place a condition of 'elif' before the numerical comparisons about rejecting any other value than 'quit', then the program will not run past that statement anymore since every value other than quit includes numerical values.
I have seen that 'try/except' might be a solution for this from reading other users questions about similar issues, but I haven't been able to do this correctly. I place the conditions for the 'if/else' in the 'else' portion of the 'try/except' block, then I still get the same issue. At that point, the value is storing integers which cause an error if a string value other than 'quit' is entered.
In any event, here is the code I am working with:
price_range = ['free','10','15'] prompt = "How old are you? " price = "The price for someone " is_old = " years old is " valid_input = "please enter a valid input" while True: movie_ticket_input = input(prompt) if movie_ticket_input == 'quit': break elif int(movie_ticket_input) < 3: print(price + str(movie_ticket_input) + is_old + price_range[0]) elif int(movie_ticket_input) < 13: print(price + str(movie_ticket_input) + is_old + price_range[1]) elif int(movie_ticket_input) <= 120: print(price + str(movie_ticket_input) + is_old + price_range[2]) else: print("Please enter a number under 120 or 'quit' to exit program")If I enter for example, the letter 'a':
Error:Traceback (most recent call last):
File "input_hw_movie_tickets.py", line 13, in <module>
elif int(movie_ticket_input) < 3:
ValueError: invalid literal for int() with base 10: 'a'
Basically, I want to end the program with value 'quit'. Print messages based on certain numbers, and if the value is either blank, or any string at all: I want a specific message to be printed.Thank you!