Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Question about running comparisons through loop from input value
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':
	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])
		print("Please enter a number under 120 or 'quit' to exit program")
If I enter for example, the letter 'a':

Traceback (most recent call last): File "", 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!
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":

    movie_ticket_input = int(movie_ticket_input)

    if movie_ticket_input > 120:
        print("Max age 120, try again")

    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]}")
How old are you? 2 The price for someone 2 years old is free How old are you? 10 The price for someone 10 years old is 10 How old are you? 25 The price for someone 25 years old is 15 How old are you? 121 Max age 120, try again How old are you? quit
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")

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Repeat question (for loop) rs74 7 263 Jun-17-2020, 03:17 PM
Last Post: rs74
  Adding string numbers, while loop and exit without input. Jose 11 617 Apr-15-2020, 08:34 AM
Last Post: Jose
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 335 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Looping unknowns with user input hw question Turkejive 4 1,587 Sep-30-2018, 04:57 PM
Last Post: Turkejive
  while loop question Tripler 4 1,004 Jul-24-2018, 06:37 AM
Last Post: buran
  How to stop an infinite loop in spyder if it's currently running? wlsa 3 14,221 Jun-30-2018, 03:27 AM
Last Post: ichabod801
  While Loop; Can Else Point Back To Initial Input Command? RodNintendeaux 9 2,489 Oct-04-2017, 05:39 AM
Last Post: gruntfutuk
  Loop question kraven 3 1,534 Sep-10-2017, 07:31 AM
Last Post: wavic
  Question about loop Pires 4 1,382 Jul-23-2017, 03:01 AM
Last Post: Pires
  Udacity while loop question liquidmetalrob 6 2,245 Jul-21-2017, 02:56 PM
Last Post: nilamo

Forum Jump:

Users browsing this thread: 1 Guest(s)