You could add in a simple if statement and raise an Error if the number isn't within the range you're expecting
while True:
try:
number1 = int(input('Number1: '))
if number1 < 1 or number1 > 10:
raise ValueError #this will send it to the print message and back to the input option
break
except ValueError:
print("Invalid integer. The number must be in the range of 1-10.")
Answer from Alan Kavanagh on Stack OverflowYou could add in a simple if statement and raise an Error if the number isn't within the range you're expecting
while True:
try:
number1 = int(input('Number1: '))
if number1 < 1 or number1 > 10:
raise ValueError #this will send it to the print message and back to the input option
break
except ValueError:
print("Invalid integer. The number must be in the range of 1-10.")
Use assert:
while True:
try:
number1 = int(input('Number1: '))
assert 0 < number1 < 10
except ValueError:
print("Not an integer! Please enter an integer.")
except AssertionError:
print("Please enter an integer between 1 and 10")
else:
break
How to set constraints on a Python Input string - Stack Overflow
Python Raw_Input in specific range - Stack Overflow
Use type hinting with bound constraints, e.g. `int[0:15]` - Ideas - Discussions on Python.org
python-constraint add constraint - Stack Overflow
Videos
Given a year, determine whether it is a leap year. If it is a leap year, return the Boolean True, otherwise return False.
Input Format
Read year, the year to test.
Constraints
1900<=y<=10^5
Output Format
The function must return a Boolean value (True/False). Output is handled by the provided code stub
Maybe I am overthinking this but what do they want us to do by putting it in ? cus I am the one who decide the input right so this is totally depend on us . I just find this redundant somehow because it is not the program but us who decide whether this work or not
Here is how you can do it with regular expressions.
Just check if input string containing only allowed chars.
import re
def is_valid_name(str):
pattern = "^[a-zA-Z\ \.\-]+$"
return bool(re.search(pattern,str))
print is_valid_name("Bob Smith")
print is_valid_name("Dr. Smith")
print is_valid_name("Anne-Marie")
print is_valid_name("Anne!Marie")
I believe you could do something like this:
def nameChecker(inputName):
# Remove valid characters from the string
validChars = [" ",".","-"]
for char in validChars:
inputName = inputName.replace(char,"")
# Check if the remaining string is valid
return inputName.isalpha()
I believe that is what you are looking for, please let me know if this works for you!
Get the input then check if the input is valid to your constraint (e.g. via an if condition). If it is not valid repeat the input (probably with notifiyng the user what was wrong with the input before) (e.g. with a while loop).
You cannot force the user to input any specific thing using raw_input(). You'll have to validate the user input and preferably use a while loop until user enters correctly.
This will work (assuming the user is not stupid to input non-digits).
i = int(raw_input('Please, enter a number between 1110 and 10000'))
while not(1110<i<10000):
i = int(raw_input('Dude, come on, read the instruction'))