Python User input [duplicate] - python

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 6 years ago.
I'm writing a game that prompts the user to input the number of rows. The problem I'm having is how do I get the program to keep prompting the user until they enters a whole number. If the user enters a letter or a float such as 2.5, the int value will not work, and thus I cannot break out of the loop. The program crashes. The int is essential so that I can check the number. The input must be even, it must be greater then or equal to 4 and less then equal to 16. Thanks!
def row_getter()->int:
while True:
rows=int(input('Please specify the number of rows:'))
if (rows%2 == 0 and rows>=4 and rows<=16):
return rows
break

You're on the right path, but you want to use a try/except block to try and convert the input to an integer. If it fails (or if the input is not in the given bounds), you want to continue and keep asking for input.
def row_getter()->int:
while True:
try:
rows=int(input('Please specify the number of rows:'))
except ValueError:
continue
else: # this runs when the input is successfully converted
if (rows % 2 == 0 and >= 4 and rows <= 16):
return rows
# if the condition is not met, the function does not return and
# so it continues the loop

I guess the pythonic way to do it would be:
while True:
try:
rows = int(input('Please specify the number of rows:'))
except ValueError:
print("Oops! Not an int...")
continue
# now if you got here, rows is a proper int and can be used
This idiom is called Easier to Ask Forgiveness than Permission (EAFP).

Could also make isint helper function for reuse to avoid the try/except in main parts of code:
def isint(s):
try:
int(s)
return True
except ValueError:
return False
def row_getter()->int:
while True:
s = input('Please specify the number of rows:')
if isint(s):
rows = int(s)
if (rows % 2 == 0 and rows >= 4 and rows <= 16):
return rows

Related

Compare string and integer in same if statement [duplicate]

This question already has answers here:
What's the canonical way to check for type in Python?
(15 answers)
How can I read inputs as numbers?
(10 answers)
Closed 6 months ago.
I have a program that takes input from the user. I want to display an invalid option message if the input is a string or it is not a number between 1 to 4. I want to check all these 3 conditions in a single if statement.
ask_option = input("Your option: ")
if int(ask_option) != int and (int(ask_option) < 1 or int(ask_option) > 4):
print("Not a valid option")
I feel the int(ask_option) != int is incorrect. But is it possible to fulfill all these 3 in a single if statement?
My code fulfills the criteria to choose between 1-4 but doesn't work to check string input. Please help
input() will always return a string unless you explicity convert it. So using ask_option = int(input("Your option: ")) will already satisfy checking if ask_option is an integer as an exception will be raised otherwise.
As for evaluating the integer, you can accomplish this in a single line such as:
if not 1 <= ask_option <= 4:
print("Not a valid option")
If you try to convert string input into an int, the program will throw ValueError.
ask_option = input("Your option: ")
try:
val = int(ask_option)
if val <= 1 or val => 4:
print("Not a valid option!")
except ValueError:
print("Not a valid option!")
ask_option = input("Your option: ")
if len(ask_option) != 1 or ask_option < '1' or ask_option > '4':
print("Not a valid option")
else:
print( "valid option")
Edit:
OP: "I want to display an invalid option message if the input is a string or it is not a number between 1 to 4."
Therefore we need to accept values ranging from 1 - 4
We know that input() accepts everything as a string
so in the if statement
first condition: len(ask_option) != 1
We invalidate any string whose length is not equal to 1 ( since we need to accept values ranging from 1-4 which are of a length 1 )
And now we are left to deal with the input value whose length is just 1 which could be anything and will be checked with the next 2 conditions.
second and third condition: ask_option < '1' or ask_option > '4'
We do string comparison ( which is done using their ASCII values )
more on string comparison: String comparison technique used by Python
I would not recommend using this approach since it's hard to tell what it does on the first look, so it's better if you use try except instead as suggested in other answers.
See this solution as just a different approach to solve OP's problem ( but not recommended to implement. )

Validating int numbers in a certain range. Python-3.x [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I am making this game where the user has to choose from 5 options. So, he must type any number from 1 to 5 to choose an option. Now here is the problem I am facing, I just can't figure out a way in which the user cannot type in any other character except the int numbers from 1 to 5, and if he does type in a wrong character, how should I show his error and make him type in the input again? Here is what I've tried:
def validateOpt(v):
try:
x = int(v)
if int(v)<=0:
x=validateOpt(input("Please enter a valid number: "))
elif int(v)>5:
x=validateOpt(input("Please enter a valid number: "))
return x
except:
x=validateOpt(input("Please enter a valid number: "))
return x
Here validateOpt is to validate the number for the option, i.e., 1,2,3,4,5. It works fine, but whenever I type 33,22, 55, or any other int number from 1 to 5 twice (not thrice or even four times, but only twice), it doesn't show any error and moves on, and that, I suppose, is wrong. I am just a beginner.
You can do this with a while loop, something like this should be a good start:
def getValidInt(prompt, mini, maxi):
# Go forever if necessary, return will stop
while True:
# Output the prompt without newline.
print(prompt, end="")
# Get line input, try convert to int, make None if invalid.
try:
intVal = int(input())
except ValueError:
intVal = None
# If valid and within range, return it.
if intVal is not None and intVal >= mini and intVal <= maxi:
return intVal
Then call it with something like:
numOneToFive = getValidInt("Enter a number, 1 to 5 inclusive: ", 1, 5)
use a while loop instead of recursion, you can check the user data and return if its valid, although you might want to consider a break out clause should the user want to exit or quit without a valid input.
def get_input(minimum, maximum):
while True:
try:
x = int(input(f"please enter a valid number from {minimum} to {maximum}: "))
if minimum <= x <= maximum:
return x
except ValueError as ve:
print("Thats not a valid input")
value = get_input(1, 5)
Use for loop in the range between 1 and 5
Try this code, it will keep prompting user till he enters 5, note that this may cause stackoverflow if recursion is too deep:
def f():
x = input("Enter an integer between 1 and 5:")
try:
x = int(x)
if x<=0 or x>5:
f()
except:
f()
f()

While loop does not break though condition is met (Collatz Sequence) [duplicate]

This question already has answers here:
Implementing the collatz function using Python
(8 answers)
Closed 5 years ago.
Note: I was trying to figure out why my while statement did not evaluate to False when the integer was, so I don't believe this is a duplicate
Doing an automating the boring stuff exercise in python where the program receives an input and reduces it to 1 using the following algorithm.
#even / 2
#odd * 3 + 1
def collatz():
print("Enter number:")
number = input()
try:
data = int(number) # Input Validation
while int(data) != 1:
if data % 2 == 0: #Number is even
data = int(data/2)
print(data)
if data % 2 == 1: # Number is odd
data = int(3*data+1)
print(data)
except:
print("Please input a valid value")
collatz()
collatz()
Instead of the while loop breaking, when the number is reduced to 1. The loop continues and it multiplies 1 by 3 and adds 1(Just as normal odd number). Btw many int conversions were done as I thought it may have returned a floating point.
So please tell me where are the unncessary int conversions and how to break it using a while statement. Any cleanup to code is appreciated
There are a few things that you should neaten up to make your code work properly and just generally better:
Rename data to n, this isn't going to make much of a difference, but I would say it just makes more sense.
There is no need to do endless conversions to int, n only needs to be converted from a string to an integer once.
Don't put your entire code in a function then call it once, instead have the main algorithm in a smaller function which you then can call from the main body of your code or even create another function to call the algorithm which handles the inputting side of things.
Use the integer division operator (//) instead of the floating point divider (/), since the number will be even, you can be sure that the decimal place will be 0.
You don't need to check if n % 2 is 0 and then on the next line check if it n % 2 is 1, you can just use an if ... else clause.
And that's about it! Here's what it looks like:
#even / 2
#odd * 3 + 1
def collatz(n):
while n != 1:
if n % 2 == 0: #Number is even
n = n // 2
else:
n = n * 3 + 1
print(n)
number = input("Enter number:")
try:
number = int(number)
collatz(number)
except ValueError:
print("Please input a valid value")
And a test shows it works (input of 24):
12
6
3
10
5
16
8
4
2
1

Prompt the user to input something else if the first input is invalid [duplicate]

This question already has answers here:
Asking the user for input until they give a valid response
(22 answers)
Closed 2 years ago.
I'm very new to Python, so forgive my newbish question. I have the following code:
[a while loop starts]
print 'Input the first data as 10 characters from a-f'
input1 = raw_input()
if not re.match("^[a-f]*$", input1):
print "The only valid inputs are 10-character strings containing letters a-f"
break
else:
[the rest of the script]
If I wanted to, instead of breaking the loop and quitting the program, send the user back to the original prompt until they input valid data, what would I write instead of break?
To go on with the next loop iteration, you can use the continue statement.
I'd usually factor out the input to a dedicated function:
def get_input(prompt):
while True:
s = raw_input(prompt)
if len(s) == 10 and set(s).issubset("abcdef"):
return s
print("The only valid inputs are 10-character "
"strings containing letters a-f.")
print "Input initial data. Must be 10 characters, each being a-f."
input = raw_input()
while len(input) != 10 or not set(input).issubset('abcdef'):
print("Must enter 10 characters, each being a-f."
input = raw_input()
Slight alternative:
input = ''
while len(input) != 10 or not set(input).issubset('abcdef'):
print("Input initial data. Must enter 10 characters, each being a-f."
input = raw_input()
Or, if you wanted to break it out in to a function (this function is overkill for this use, but an entire function for a special case is suboptimal imo):
def prompt_for_input(prompt, validate_input=None, reprompt_on_fail=False, max_reprompts=0):
passed = False
reprompt_count = 0
while not (passed):
print prompt
input = raw_input()
if reprompt_on_fail:
if max_reprompts == 0 or max_reprompts <= reprompt_count:
passed = validate_input(input)
else:
passed = True
else:
passed = True
reprompt_count += 1
return input
This method lets you define your validator. You would call it thusly:
def validator(input):
return len(input) == 10 and set(input).subset('abcdef')
input_data = prompt_for_input('Please input initial data. Must enter 10 characters, each being a-f.', validator, True)

How do I make my script take only numeric inputs without screwing it up

I've been banging my head against the wall with this task and I can't seem to figure it out for the life of me.
I want to write a script that would prompt a user to input a number and store that number in a list every time after they input the number. When the user would just press enter and not input anything, the script would then print out the highest number in the list.
This is what I wrote so far:
x = 0
mylist = []
while x != '':
x = input("enter a number:")
mylist.append(x)
if x == '':
print(max(mylist))
There are two problems with this code that I can see:
The user can input a string (I tried fixing that by saying x = int(input(...), but that only lead to a new error:
ValueError: invalid literal for int() with base 10: ''
print(max(mylist)) only prints out the number with the largest initial integer. For instance, if the user inputs 51 and 112, it would print out 51.
Thank you for taking your time in helping me out.
Use a different variable to test the While. Check isnumeric() for the input, and if it is numeric, convert it as you append it to your array:
keepMoving = True
mylist = []
while keepMoving:
x = input("enter a number:")
if x.isnumeric():
mylist.append(int(x))
else:
keepMoving = False
print(max(mylist))
I suggest you use a try statement to check if the input is an integer. The following is an implementation which attempts to convert the user input into an integer, and append that to the list. In the result of a ValueError (a non-int input), it will print the largest integer in the list. Using raw_input instead of input is also a good practice here, to prevent python from trying and failing to convert the input on its own.
x=0
mylist=[]
while x!='':
x = raw_input("enter a number:")
try:
x = int(x)
mylist.append(x)
except ValueError:
if x == '':
print(max(mylist))
This prevents the user from adding non-integers to the list, and is a better way to handle unexpected user input. As an added bonus, this method also makes it easy to add more conditions in the future.

Categories

Resources