Not wanting to use if else but try except block in python - 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)

Related

how do i raise an exception when i prompt user to enter a number

i want to get input from user to fill the empty list. if user enters a negative integer- it will not get appended rather a ValueError will be raised.
nameList = []
count = 0
try:
while count < 5:
num = int(input('Enter a number: '))
if num < 0:
except NameError:
print('Positive integers only!')
numList.append(num)
count = count + 1
print(numList)
I tried adding assertion after the try and before the while but I got syntax error since num is not defined.
Use raise to raise an exception:
num = int(input('Enter a number: '))
if num < 0:
raise ValueError('Positive integers only!')
Use except to handle an exception raised in the corresponding try (that is, the except and try need to be at the same indentation level). If you wanted to handle the ValueError by printing an error message and continuing the loop, you might do:
try:
num = int(input('Enter a number: '))
if num < 0:
raise ValueError('Positive integers only!')
except ValueError as e:
# This will happen if the user entered something
# non-numeric OR a valid but negative number.
print(e)
continue
numList = []
count = 0
while count < 5:
try:
num = int(input('Enter a number: '))
if num < 0:
raise ValueError('Positive integers only!')
numList.append(num)
count += 1
except ValueError as e:
print(e)
print(numList)
There's little point to raising an exception that you intend to immediately catch. Just use continue to go back to the top of the loop body.
nameList = []
count = 0
while count < 5:
try:
num = int(input('Enter a number: '))
except ValueError:
print("Not an integer")
continue
if num < 0:
print('Positive integers only!')
continue
numList.append(num)
count = count + 1
While you are at it, use a for loop to repeat the body (successfully) 5 times, without the need to explicitly increment count:
for _ in range(5): # _ instead of count to emphasize that you don't use the variable
try:
num = int(input('Enter a number: '))
except ValueError:
print("Not an integer")
continue
if num < 0:
print('Positive integers only!')
continue
numList.append(num)
I should soften this a little. If you intend to handle an exception raised by int and your own explicit exception in exactly the same way, I wouldn't say "no" to using raise ValueError("Not an integer") in the try block. But, this is an opportunity to provide a different error message for non-integers and negative integers, so I would still recommend separating the two checks as above.

while loop until input is has fulfilled two statements

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

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)

Python3: creating error for input of negative

Taking an intro course and need to create an over/under guessing game. I want to fine-tune my user inputs by creating an error if someone inputs a negative or non-integer. I have the non-integer error reporting correctly, and the negative loops back correctly, but the negative will not print my error message.
#Number of plays
def get_plays(msg):
while True:
try:
x = (int(input(msg)))
except ValueError:
print ("Integer numbers only please.")
except:
if x <=0:
print ("Positive numbers only please.")
i = get_plays("\nHow many times would you like to play?")
print ("The game will play " +str(i)+" times.")
Separately, if I wanted to use a similar setup to produce an error for any negative non-integer number between 1 and 20, how would this look?
Try:
def get_plays(msg):
while True:
try:
x = (int(input(msg)))
if x <=0:
print("Positive numbers only please.")
continue
if x not in range(20):
print("Enter a number between 1 - 20.")
continue
return x
except ValueError:
print("Integer numbers only please.")
It will accept only positive numbers between 1 to 20
The problem is this section
except:
if x <=0:
Empty except clauses trigger on any error that hasn't been caught yet, but negative numbers don't trigger an exception. You want something like this instead. Notice that in the try clause we can just proceed as if x is an int already, because we can assume that no ValueError was thrown.
def get_plays(msg):
while True:
try:
x = (int(input(msg)))
if x <=0:
print ("Positive numbers only please.")
else:
return x
except ValueError:
print ("Integer numbers only please.")

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