Your code would become:

def Survey():

    print('1) Blue')
    print('2) Red')
    print('3) Yellow')

    while True:
        try:
            question = int(input('Out of these options\(1,2,3), which is your favourite?'))
            break
        except:
            print("That's not a valid option!")

    if question == 1:
        print('Nice!')
    elif question == 2:
        print('Cool')
    elif question == 3:
        print('Awesome!')
    else:
        print('That\'s not an option!')

The way this works is it makes a loop that will loop infinitely until only numbers are put in. So say I put '1', it would break the loop. But if I put 'Fooey!' the error that WOULD have been raised gets caught by the except statement, and it loops as it hasn't been broken.

Answer from HarryCBurn on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ how-to-take-integer-input-in-python
How to take integer input in Python? - GeeksforGeeks
April 3, 2026 - n = int(input("Enter the size of list: ")) lst = list(map(int, input( "Enter the integer elements of list(Space-Separated): ").strip().split()))[:n] print('The list is:', lst)
Discussions

Question| how to make the user enter only integers
prompt = 'Please enter a number' inpt = input(prompt) while not inpt.isnumeric(): inpt = input(prompt) More on reddit.com
๐ŸŒ r/learnpython
12
1
March 21, 2021
How to make users only enter integer values in Python program - Stack Overflow
I have a homework assignment that I am very confused about. I have to write a program allowing users to input the number of eggs that they are buying. The program will then tell them how many boxes... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to accept only integer or float inputs?

I don't understand why you don't just use floats?

while True:
            try:
                time = float(raw_input("How much time has elapsed since your last stop (in hours)?"))
            except ValueError:
                print("Enter a valid number of hours!")
                continue

By typing an integer value, it'll just turn it into a float anyway.

More on reddit.com
๐ŸŒ r/learnpython
14
5
February 23, 2016
How take a input only as integer in python?
Learn more efficiently, for free: ยท Introduction to Python More on sololearn.com
๐ŸŒ sololearn.com
4
1
March 15, 2018
๐ŸŒ
IncludeHelp
includehelp.com โ€บ python โ€บ asking-the-user-for-integer-input-in-python-limit-the-user-to-input-only-integer-value.aspx
Asking (Limit) the user for only integer input in Python
input() function can be used for the input, but it reads the value as a string, then we can use the int() function to convert string value to an integer.
๐ŸŒ
Real Python
realpython.com โ€บ python-input-integer
How to Read Python Input as Integers โ€“ Real Python
March 18, 2026 - In Python, you handle exceptions in a try โ€ฆ except block. By wrapping the try โ€ฆ except block inside a while loop, you can guarantee that only integers will make it through. A mechanism like this is sometimes called a retry loop.
๐ŸŒ
Bobby Hadz
bobbyhadz.com โ€บ blog โ€บ python-only-allow-integer-user-input
How to take Integer user input in Python | bobbyhadz
Use the input() function to take input from the user. Use a try/except statement to make sure the input value is an integer.
๐ŸŒ
Pierian Training
pieriantraining.com โ€บ home โ€บ python tutorial: how to take an integer input in python
Python Tutorial: How to take an integer input in Python - Pierian Training
April 12, 2023 - In Python, we can take input from the user using the `input()` function. The `input()` function reads a line of text entered by the user and returns it as a string. To take an integer input, we need to convert the string input to an integer.
Find elsewhere
๐ŸŒ
Quora
quora.com โ€บ How-can-we-accept-integer-values-only-as-input-for-a-program-written-in-Python
How can we accept integer values only as input for a program written in Python? - Quora
Answer (1 of 4): You can import the function inputInt() from the module pyinputplus and use it to verify that a given input is an integer. Anything other than an integer (positive or negative) will generate an error message and wait for the user to re-enter a valid integer. Here is a Python 3 ex...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ article โ€บ how-to-take-integer-input-in-python
How to take integer input in Python?
import sys # Convert command line arguments to integers if len(sys.argv) > 1: numbers = [int(arg) for arg in sys.argv[1:]] print("Command line integers:", numbers) print("Sum:", sum(numbers)) print("Average:", sum(numbers) / len(numbers)) else: print("No arguments provided") # Run as: python script.py 10 20 30 40 ยท Command line integers: [10, 20, 30, 40] Sum: 100 Average: 25.0 ยท Always handle cases where input might not be a valid integer ?
๐ŸŒ
YouTube
youtube.com โ€บ kunal attri
How to take only Integer Input in Python - YouTube
You can easily take an integer only input in Python by using a recursive function which continously takes input and convert to integer and return that intege...
Published: February 22, 2021
Views: 371
๐ŸŒ
Reddit
reddit.com โ€บ r/learnpython โ€บ how to accept only integer or float inputs?
r/learnpython on Reddit: How to accept only integer or float inputs?
February 23, 2016 -

My current code accepts integers that are greater than 0, which is what I want, but I also want to accept floats/decimals as well. How would I do this? Here is my current code:

while True:
            try:
                time = int(input("How much time has elapsed since your last stop (in hours)?"))
            except ValueError:
                print("Enter a valid number of hours!")
                continue
            if time > 0:
                givenTime[2] = time
                break

Indentation is off here for some reason, but believe me when I say that the indentation in the actual code is correct..

Can I just do time = int or float(input("How much time has elapsed since your last stop (in hours)?")) or would this be wrong? Any help would be appreciated.

edit: nope,

time = int or float(input("How much time has elapsed since your last stop (in hours)?"))

doesn't work.

๐ŸŒ
101 Computing
101computing.net โ€บ home โ€บ python challenges โ€บ python tip: validating user input as number (integer)
Python Tip: Validating user input as number (Integer) - 101 Computing
July 14, 2025 - To use them as integers you will need to convert the user input into an integer using the int() function. e.g. ... This line of code would work fine as long as the user enters an integer.
๐ŸŒ
EE-Vibes
eevibes.com โ€บ computing โ€บ how-to-take-integer-input-in-python
How to take integer input in Python?
December 6, 2021 - In this post, We will perceive how to take whole number contribution to Python. As we realize that Python worked in input() work consistently returns a str(string) class object. So for taking whole number info we need to type cast Read More ...
๐ŸŒ
Hyperskill
hyperskill.org โ€บ university โ€บ python โ€บ input-and-input-in-python
Python input() Function: Get User Input with Examples
June 5, 2026 - For instance if we aim to treat the input as an integer we can utilize the int() function for conversion. Ensuring that the input is converted into the data type is fundamental, for ensuring smooth operation of the program. This practice helps in preventing errors and facilitates proper ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ taking-input-in-python
Taking Input in Python - GeeksforGeeks
... You can optionally provide ... default. If you need another data type (like integer or float), you must convert it manually using typecasting....
Published: May 29, 2026
๐ŸŒ
Educative
educative.io โ€บ blog โ€บ how-to-take-integer-input-in-python
How to take integer input in Python
The input() function in Python is designed to accept user input, returning whatever the user types as a string. This necessitates the conversion of this string to an integer when numerical input is needed.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to take int input in Python โ€” safely and correctly - YouTube
Trying to get numeric input from your user? If you're running int on the result of the input function, then you're likely going to have problems. How can we ...
Published: July 13, 2025