Python Forum
Question about running comparisons through loop from input value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about running comparisons through loop from input value
#1
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':

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!
Reply
#2
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]}")
            break
output:
Output:
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
Reply
#3
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,816 Nov-12-2022, 07:08 PM
Last Post: deanhystad
  Please check whether the code about the for loop question is correct. (SyntaxError) lilliancsk01 10 2,553 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,777 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Repeat question (for loop) rs74 7 6,441 Jun-17-2020, 03:17 PM
Last Post: rs74
  Adding string numbers, while loop and exit without input. Jose 11 7,449 Apr-15-2020, 08:34 AM
Last Post: Jose
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,421 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Looping unknowns with user input hw question Turkejive 4 4,984 Sep-30-2018, 04:57 PM
Last Post: Turkejive
  while loop question Tripler 4 2,936 Jul-24-2018, 06:37 AM
Last Post: buran
  How to stop an infinite loop in spyder if it's currently running? wlsa 3 24,673 Jun-30-2018, 03:27 AM
Last Post: ichabod801
  While Loop; Can Else Point Back To Initial Input Command? RodNintendeaux 9 6,244 Oct-04-2017, 05:39 AM
Last Post: gruntfutuk

Forum Jump:

User Panel Messages

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