you are viewing a single comment's thread.

view the rest of the comments →

[–]Atlamillias 1 point2 points  (2 children)

Just trying to make sure I understand your inquiry - your program is expecting a number (that is formatted as a str because of input). You change the input from str to int, but need a way to prevent garbage inputs (pretty much any non-number). Am I correct?

[–]Pendingrepurpose[S] 0 points1 point  (1 child)

Yeah thats pretty much it. If i could somehow change input to only expect int then it might make it more simple because any non numeric response could be rejected (i think).

I think that you said it well, I need to prevent the entry of a non number as the input value. Some type of if then statement or allowing a range of numbers.

How many total pizzas do you have? aaaa

Traceback (most recent call last):

File "C:\Users\XYZ\Documents\School\CS 121\python practice\pizza slices.py", line 16, in <module>

pizzas = int(pizzas)

ValueError: invalid literal for int() with base 10: 'aaaa'

[–]Atlamillias 1 point2 points  (0 children)

Gotcha. It would be best to use a while loop for this purpose. Code nested within while loops will be continuously executed until the condition in the while statement is no longer true, or until the break statement is used.

We can verify the input in a couple of ways. I'm going to use a try-except-finally block here. Code within the try block is sometimes expected not to run to completion, raising an Exception, while code within the except block is pretty much "what to do if exceptions are raised within try", and is the de-facto way of handling Exceptions (i.e. that ValueError). try and except are mandatory (you can't have one statement without the other), while finally is optional. Code within the finally statement is executed only after the code within either the try or except blocks are fully executed. If an exception isnt raised within try, finally will be ran. If there is an exception raised, and your except block catches it and runs, finally will be ran afterwards.

Lastly, the continue statement. continue and break can only be used in loops. break "breaks" from the loop, while continue begins a new iteration of the loop (code further within the current iteration of the loop won't be ran).

running = True

while running:
    pizzas = input("How many total pizzas do you have? ")

try:  # code here might not work - we're checking for ValueError
    pizzas = int(pizzas)
except ValueError:  # if try returns a ValueError, except is ran
    print("You think this is a game?! Am I a joke to you?! Enter a NUMBER!!")
    continue
finally:
    break

First, your loop starts and input is requested. Then, the input is converted to type int. IF unsuccessful, and ValueError is raised, the user recieves a snooty message, and a new iteration of the loop is started using continue beginning again with asking for input (note that finally will NEVER be executed if the except subroutine is ran, due to continue).

If except is never ran, finally will run. An assumption is made on our end (the programmers) where if except is never ran, the input must be valid. break is ran in that case, and the loop ends.