Question about running comparisons through loop from input value - 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: Question about running comparisons through loop from input value (/thread-21754.html) |
Question about running comparisons through loop from input value - Sunioj - Oct-13-2019 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: 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': 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! RE: Question about running comparisons through loop from input value - Larz60+ - Oct-13-2019 Try this: import sys price_range = ['free','10','15'] age_breaks = [3,13,121] prompt = "How old are you? " price = None while True: movie_ticket_input = input("How old are you? ") if movie_ticket_input == "quit": break movie_ticket_input = int(movie_ticket_input) if movie_ticket_input > 120: print("Max age 120, try again") continue for n, maxage in enumerate(age_breaks): if movie_ticket_input < maxage: print(f"The price for someone {movie_ticket_input} years old is {price_range[n]}") breakoutput:
RE: Question about running comparisons through loop from input value - jefsummers - Oct-15-2019 That won't catch bad data entry. You are right in the first post that a try...except will be helpful. If you take Larz60+ code and add a try except clause around line 17 that should catch bad string entry: try : movie_ticket_input = int(movie_ticket_input) except : print("Enter a valid integer less than 120") continue |