while loop until input is has fulfilled two statements - python

def ask_input(prompt, error):
while True:
value = input(prompt)
try:
int(value) > 1
break
except ValueError:
print(error)
else:
return value
So I want to make simple function that returns value if its integrel and greater than 1. So far the function seems to accept anything I put in. Do I need to make multiple loops or can I integrate both of statement in a while loop?

Use if statement:
while True:
value = input(prompt)
try:
if int(value) > 1:
return int(value) # return the value if condition met
except ValueError as error:
print(error)

Your code prompts the user for anything that can be converted to an integer, whether or not it is > 1, then return None. This is not what you intended.
There are 2 different problems here:
int(value) > 1
If the input value is not parsable as an integer, then this will raise a ValueError that you catch and handle properly.
If the input value is parsable as an integer, then int(value) will convert it to an actual integer which will be compared to 1. The result of this comparison, however, is completely ignored since it is not stored in a variable or used.
Then the break will immediately leave the while loop, then the end of ask_input is reached, without a return statement. Because of that the else clause of your try is ignored.
I suggest the following code:
def ask_input(prompt, error):
while True:
value = input(prompt)
try:
if int(value) > 1:
return value
except ValueError:
print(error)

Use a try/except to accept only integers. Then test the input for the greater than 1 condition using an if condition, return the value if it satisfies the condition. It's better to use raw_input() in this case as you want an integer literal as input, where as input() can also evaluate expressions potentially leading to unsanitized input.
def ask_input(prompt, error):
while True:
try:
value = int(raw_input(prompt))
if value > 1: # Return the number if it's greater than 1.
return value
except ValueError: # Catch exception if value is not a base 10 int.
print error

Related

Avoid ValueError outside a while loop?

In the second line, I am trying to make it not crash if a string is entered but can't find a way to use an exception or something similar. In the while loop it works normally as the exception deals with this case.
number = 0 #this to be removed
number = (float(input('pick a number'))) #I want to make this not crash if a string is entered.
while number not in [100, 200, 3232]:
try:
print('wrong number ')
number = (float(input('pick a number;'))) #Here it does not crash if a string is entered which is fine
except ValueError:
print("retry")
while followed by a variable condition often ends up with bugs/repeating code => bad maintenability.
Use a while True condition to avoid repeating your code, then break out the loop when it's valid.
while True:
try:
number = float(input('pick a number;'))
if number in [100, 200, 3232]:
break
print("wrong number")
except ValueError:
print("illegal number, retry")
note: be careful with float equality in the general case (see also strange output in comparison of float with float literal, Is floating point math broken?)
Maybe create a function to take input:
def get_input():
while True:
a = input("Pick a number: ")
try:
return float(a)
except:
pass

Not wanting to use if else but try except block in python

I am trying to write a program that does the following in python:
accept three arguments: a prompt, a low acceptable limit, and a high acceptable limit;
if the user enters a string that is not an integer value, the function should emit the message Error: wrong input, and ask the user to input the value again;
if the user enters a number which falls outside the specified range, the function should emit the message Error: the value is not within permitted range (min..max) and ask the user to input the value again;
if the input value is valid, return it as a result.
I don't want to use if-else. I am learning catching exceptions in python. i want to use try-except
I have written the following code
def readint(prompt, min, max):
try:
theNum = int(input(prompt))
if theNum in range(min, max+1):
return theNum
except ValueError:
return "Error: wrong input. The value is not within the permitted range (-10..10)"
readint(prompt, min, max)
v = readint("Enter a number from -10 to 10: ", -10, 10)
print("The number is:", v)
The output prints out any number in the mentioned range but when a number not within the range is entered, the output says "The number is: None" and it does not re-prompt me to enter another number
min and max are keywords in python. I would avoid using them as variable names.
def readint(prompt, minimum, maximum):
while True:
try:
value = int(input(prompt))
assert minimum <= value <= maximum
prompt = "Try again: "
except ValueError:
print("Error: Not an integer")
except AssertionError:
print("Error: Value not within range")
else:
break
return value
minimum = -10
maximum = 10
value = readint(f"Enter a number between {minimum} and {maximum}: ", minimum, maximum)
Here's a typical retry loop that uses try/except:
def thing_to_retry():
while True:
try:
return thing()
except SomeError as e:
print("Whoopsie! Error: {}".format(e))
Since the try/except is within the loop, any exception you catch will cause another loop iteration. Success will cause the function to return from the loop.
def readint(prompt, min, max):
doIt = True
while doIt:
try:
userInput = int(input(prompt))
if userInput in range(min, max+1):
doIt = False
return userInput
else:
raise AssertionError
except ValueError:
print("Error: wrong input.")
except AssertionError:
print("The value is not within the permitted range (-10..10)")
except:
doIt = True
v = readint("Enter a number from -10 to 10: ", -10, 10)
print("The number is:", v)

How to get only numbers as a response in python?

I'm very new to python, in fact, to programming in general. I'm trying to do a program that compares three numbers and determines which one is smaller. I got the code done, but now i need to make it only accept numbers, and still running when it finds a literal value. For example, code will be ok if you put any number, but when you type a string value it crashes. here is the code
num1=0;
num2=0;
num3=0;
num1=int((raw_input("Type below the first number \n")));
num2=int((raw_input("Type below the second number\n")));
num3=int((raw_input("Type below the third number \n")));
if (num1<num2) and (num1<num3):
print "%i is the smallest number of all three"% num1;
elif (num2<num1) and (num2<num3):
print "%i is the smallest number of all three"% num2;
elif (num3<num2) and (num3<num1):
print "%i is the smallest number of all three"% num3;
elif (num1==num2) or (num1==num3) or (num2==num3):
print "Two equal numbers have been written.";
A simple while loop. You may put this in a function.
while True:
try:
num1=int((raw_input("Type below the first number \n")))
break
except ValueError: # catch the *specific* exception
print("Enter numbers only")
Read more on exceptions:
Handling Exceptions in Python
The most important point is probably to separate concerns, ie. keeping user input separate from validation. You've gotten a couple of good solutions using exceptions. Here is a solution that manually validates the input:
def is_int(strval):
"""A string represents an int if all characters are digits.
Returns True if strval represents a valid int, otherwise False.
"""
for ch in strval:
if not ch.isdigit(): # if '0' <= ch <= '9':
return False
return True
def read_int(prompt='Type number: '):
"""Read one integer from user.
"""
while True:
val = raw_input(prompt)
if is_int(val):
return int(val, 10) # the 10 is the radix (base)
else:
print 'That is not a number.., try again'
numbers = []
for pos in 'first', 'second', 'third':
numbers.append(read_int('Type %s number: ' % pos))
to use the exception handling code, all you have to do is change is_int:
def is_int(strval):
try:
int(strval, 10)
except ValueError:
# int() raised an exception, so strval is not an int
return False
else:
# int() didn't raise an exception, so it is an int
return True
it is also possible to be a bit more concise when finding the smallest:
def smallest(a, b, c):
if b > a < c: # if a is less than both b and c..
return a
if a > b < c:
return b
if a > c < b:
return c
return "Error: two of the numbers must be equal!"
you can call it like so:
smallest(numbers[0], numbers[1], numbers[2])
but Python has a shorthand that does exactly that which looks like:
smallest(*numbers)
def getNumber(i):
try:
num = int(raw_input("type in number %s : " %i))
return num
except Exception as e:
print('This is not a valid number')
return getNumber(i)
numbers = []
for i in range(1,4):
numbers.append(getNumber(i))
print(numbers)
You can simply add the input getting code in try except block and handle the ValueError exception with some meaningful code.
try:
num1=int((raw_input("Type below the first number \n")));
num2=int((raw_input("Type below the second number\n")));
num3=int((raw_input("Type below the third number \n")));
except ValueError:
print "Please enter integer only."
exit(-1)

Why doesn't this code trigger a ValueError?

I'm trying to trigger an error when the "chunk_size" is zero, or greater than the length of the "sequence." When I call this function with a chunk_size of 4, and a sequence that equals "123" it doesn't throw an error. What did I do wrong?
def slices(sequence,chunk_size):
position=0
mini_list=[]
answer=[]
while chunk_size<=len(sequence) and chunk_size>0:
try:
for char in sequence:
if len(sequence[position:position+chunk_size])==chunk_size:
mini_seq = sequence[position:position+chunk_size]
for digit in mini_seq:
mini_list.append(int(digit))
answer.append(mini_list)
mini_list=[]
position+=1
return answer
break
except ValueError:
print "Oops! That was no valid number. Try again..."
print slices("012", 4)
It doesn't throw a ValueError, because you raise it within the while block, and the while block is entered only when the required condition of chunk_size being < 0 or > len(str) is not met.
Hence, to correct this, move the error raising part of your code out of the while loop. In fact, your placement of the condition within while is wrong, instead convert it to an if statement. Also, a break before a return statement won't make any sense.
So your code becomes
while True:
try:
if chunk_size<=len(sequence) and chunk_size>0:
for char in sequence:
if len(sequence[position:position+chunk_size])==chunk_size:
mini_seq = sequence[position:position+chunk_size]
for digit in mini_seq:
mini_list.append(int(digit))
answer.append(mini_list)
mini_list=[]
position+=1
return answer
else:
raise ValueError
except ValueError:
print "Oops! That was no valid number. Try again..."
Further, instead of raising an error to validate whether the data is in the right format, you can do away with raising the exception itself, and achive the same result with a simple if-else block:
while True:
if chunk_size<=len(sequence) and chunk_size>0:
for char in sequence:
if len(sequence[position:position+chunk_size])==chunk_size:
mini_seq = sequence[position:position+chunk_size]
for digit in mini_seq:
mini_list.append(int(digit))
answer.append(mini_list)
mini_list=[]
position+=1
return answer
else:
print "Oops! That was no valid number. Try again..."

Python excepting input only if in range

Hi I want to get a number from user and only except input within a certain range.
The below appears to work but I am a noob and thought whilst it works there is no doubt a more elegant example... just trying not to fall into bad habits!
One thing I have noticed is when I run the program CTL+C will not break me out of the loop and raises the exception instead.
while True:
try:
input = int(raw_input('Pick a number in range 1-10 >>> '))
# Check if input is in range
if input in range(1,10):
break
else:
print 'Out of range. Try again'
except:
print ("That's not a number")
All help greatly appreciated.
Ctrl+C raises a KeyboardInterruptException, your try … except block catches this:
while True:
try:
input = int(raw_input('Pick a number in range 1-10 >>> '))
except ValueError: # just catch the exceptions you know!
print 'That\'s not a number!'
else:
if 1 <= input < 10: # this is faster
break
else:
print 'Out of range. Try again'
Generally, you should just catch the exceptions you expect to happen (so no side effects appear, like your Ctrl+C problem). Also you should keep the try … except block as short as possible.
There are several items in your code that could be improved.
(1) Most importantly, it's not a good idea to just catch a generic exception, you should catch a specific one you are looking for, and generally have as short of a try-block as you can.
(2) Also,
if input in range(1,10):
would better be coded as
if 1 <= input < 10:
as currently function range() repeatedly creates a list of values form 1 to 9, which is probably not what you want or need. Also, do you want to include value 10? Your prompt seems to imply that, so then you need to adjust your call to range(1, 11), as the list generated will not include the upper-range value. and the if-statement should be changed to if 1 <= input <= 10:
You can use this code:
def read_int(prompt, min, max):
while True:
try:
val = int(input(prompt))
except ValueError:
print('Error: wrong input')
else:
if(min <= val < max): # this is faster
break
else:
print('Error: the value is not within permitted range (min..max)')
return val
v = read_int("Enter a number from -10 to 10: ", -10, 10)
print("The number is:", v)

Categories

Resources