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


Messages In This Thread
Question about running comparisons through loop from input value - by Sunioj - Oct-13-2019, 12:04 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Many iterations for loop question adesimone 9 1,744 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,486 Nov-08-2022, 01:25 PM
Last Post: deanhystad
  Beginner Python Question: FIzz Buzz using while loop camoyn13 2 1,729 Sep-20-2022, 09:00 AM
Last Post: deanhystad
  Repeat question (for loop) rs74 7 6,355 Jun-17-2020, 03:17 PM
Last Post: rs74
  Adding string numbers, while loop and exit without input. Jose 11 7,378 Apr-15-2020, 08:34 AM
Last Post: Jose
  If Statements and Comparisons returns a syntax error DarkAlchemyXEX 2 2,385 Jan-02-2020, 01:25 PM
Last Post: DarkAlchemyXEX
  Looping unknowns with user input hw question Turkejive 4 4,944 Sep-30-2018, 04:57 PM
Last Post: Turkejive
  while loop question Tripler 4 2,889 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,608 Jun-30-2018, 03:27 AM
Last Post: ichabod801
  While Loop; Can Else Point Back To Initial Input Command? RodNintendeaux 9 6,202 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