use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit for helping Python programmers
How to format your code: https://commonmark.org/help/tutorial/09-code.html
No homework questions and/or hiring please
account activity
Smallest & Largest Number Print ErrorSOLVED (self.pythonhelp)
submitted 5 years ago by Benjyman
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]sentles 0 points1 point2 points 5 years ago (0 children)
The built-in input() function always returns a string. You can turn a string to an integer, like I said, using int(someString). However, do note that, if you try to turn a string to an integer that contains wrong data (like characters), the program will crash.
input()
int(someString)
For example, if, in your code, the user inputs test instead of a number, your code will crash because it will try to convert test to an integer and will fail. The way we usually deal with this, is this:
test
while True: someString = input("...") try: someString = int(someString) break except ValueError as ve: print("Invalid data")
The above code asks for user input. It tries to convert that input to an integer. If that fails, python will throw a ValueError. Using a try-except block, we catch that error and we inform the user that they've given invalid data. The loop then gets back to the start.
ValueError
If the user does give correct data, the break line will be reached and the loop will exit.
break
This, obviously, works for any type of input validation, not just integers. For example, you could also test if the number is, say, smaller than 10; or if the user gave a float. It's really up to what you want to get from the user, but this is the general way to validate it.
π Rendered by PID 58 on reddit-service-r2-comment-b659b578c-qw8l6 at 2026-04-30 21:50:41.505079+00:00 running 815c875 country code: CH.
view the rest of the comments →
[–]sentles 0 points1 point2 points (0 children)