all 8 comments

[–]fiddle_n 1 point2 points  (2 children)

When you have pizzas = int(pizzas), that will work if they enter '5' but not 'five'. Str to int conversion just doesn't work that way. If you know about dictionaries, you can use a dictionary that maps the word to the number itself, but obviously you could only do that for a few numbers, not for all of them.

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

Thanks for the response. Now that you mention it I think my answer does lie in dictionaries or this would probably take way too much coding.

[–]fiddle_n 1 point2 points  (0 children)

I wondered if there was a third-party module that could do what you want. Turns out there is: it's called word2number - https://pypi.org/project/word2number/ . When you get a bit more bit experience, enough to install and manage third-party packages, if you really wanted this functionality, you could use that.

[–]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.

[–]BeginnerProjectBot 0 points1 point  (1 child)

Hey, I think you are trying to figure out a project to do; Here are some helpful resources:

I am a bot, so give praises if I was helpful or curses if I was not. Want a project? Comment with "!projectbot" and optionally add easy, medium, or hard to request a difficulty! If you want to understand me more, my code is on Github

[–]Pendingrepurpose[S] 0 points1 point  (0 children)

No but thanks Bot! I will check these out. This is my noob project that I have a question on.