Reputation: 85
I'm having trouble protecting my script from crashing when the user inputs an invalid value to store into a 'number' variable.
Purpose of program: Allow user to enter integers, stores them into a list, then finds and prints the highest number. Does not allow for strings or floats in input.
Functions:
getNum(prompt)
- to test and convert the inputted value to integer and returns y.swap(A, x, y)
- simple swap routine function to sort the data. (Not currently in use)sortNum(A)
- to sort and use the Swap routine in the list.maxIt(numbers)
- to find the highest number in the list. (IN USE), I don't think I even need to sort the list if I'm using this max function.main()
Code:
def getNum(prompt):
# Function to call when asking for the numbers later.
# Try/except to catch crashes if failing to store integer.
try:
x = input(prompt)
y = int(x)
return y
# Message to print when excepting.
except:
print("I was expecting an integer number, please try again... \n")
# Function to swap to highest # in list 'numbers'.
def swap(A, x, y):
temp1 = A[x]
A[x] = A[y]
A[y] = temp1
# Function to perform the looping/swap routine.
# Program can find max with separate function.
## This function is to sort the numbers in the list.
def sortNum(A):
for i in range(len(A)):
for k in range(len(A) - 1):
first = k
second = k + 1
if (A[first] > A[second]):
# Uses Swap function
swap(A, first, second)
# Function to find the highest number in the list.
def maxIt(numbers):
maxNum = numbers[0]
for i in numbers:
if i > maxNum:
maxNum = i
return maxNum
# Start main
def main():
# Creates the numbers array.
numbers = []
# Starts the loop to enter numbers into the array.
done = False
while not done:
numInput = getNum("Please enter an integer or < 0 to finish >: ")
# Stores every input from numInput into numbers.
numbers.append(numInput)
# Test condition to break out of loop '0'.
if numInput is 0:
# Prints the resulting max number once finished.
print("The maximum value is: " + str(maxIt(numbers)))
# Flag to stop the program once finished.
done = True
main()
Current Output when not failing getNum's test (str or float):
Please enter an integer or < 0 to finish >: 222
Please enter an integer or < 0 to finish >: 333
Please enter an integer or < 0 to finish >: 444
Please enter an integer or < 0 to finish >: 555
Please enter an integer or < 0 to finish >: 666
Please enter an integer or < 0 to finish >: 777
Please enter an integer or < 0 to finish >: 888
Please enter an integer or < 0 to finish >: 999
Please enter an integer or < 0 to finish >: 0
The maximum value is: 999
Errors when entering a str or float into getNum/numInput:
Please enter an integer or < 0 to finish >: 222
Please enter an integer or < 0 to finish >: 333
Please enter an integer or < 0 to finish >: 444
Please enter an integer or < 0 to finish >: test
I was expecting an integer number, please try again...
Please enter an integer or < 0 to finish >: 555
Please enter an integer or < 0 to finish >: 666
Please enter an integer or < 0 to finish >: 0
Traceback (most recent call last):
File "C:\Users\Bar\Desktop\IS115\Peretz_A9.py", line 64, in <module>
main()
File "C:\Users\Bar\Desktop\IS115\Peretz_A9.py", line 59, in main
print("The maximum value is: " + str(maxIt(numbers)))
File "C:\Users\Bar\Desktop\IS115\Peretz_A9.py", line 37, in maxIt
if i > maxNum:
TypeError: '>' not supported between instances of 'NoneType' and 'int'
Upvotes: 1
Views: 1414
Reputation: 2838
Well, the problem is that your function getNum
return a None when the number is not valid, try the following for a quick fix:
if numInput is not None:
numbers.append(numInput)
But i also suggest that you read:
Why is "except: pass" a bad programming practice?
And try to avoid the empty except clause in your code!
Upvotes: 2
Reputation: 7055
The problem is your try..except
not returning anything (returning None
) in the exceptional case:
def getNum(prompt):
# Function to call when asking for the numbers later.
# Try/except to catch crashes if failing to store integer.
try:
x = input(prompt)
y = int(x)
return y
# Message to print when excepting.
except:
print("I was expecting an integer number, please try again... \n")
# Returns None resulting in your error
You can avoid this by placing your input in a loop:
def getNum(prompt):
# Function to call when asking for the numbers later.
# Try/except to catch crashes if failing to store integer.
while True:
try:
x = input(prompt)
y = int(x)
return y
# Message to print when excepting.
except ValueError:
print("I was expecting an integer number, please try again... \n")
Upvotes: 1
Reputation: 19144
Your getNum
returns either an int or None (after printing the message). main
unconditionally appends the return value even if None. Hence the comparison message. Change
numbers.append(numInput)
to
if isinstance(numInput, int):
numbers.append(numInput)
Upvotes: 0
Reputation: 1334
The problem is located in the function getNum
. Actually, when you are typing some text in the input, the function getNum
reach an exception because you are trying to cast not digit str
into int
then the message is printed and the function returns... None
. None
is stored into numInput then added to the list. You know what follows.
In order to make this work, you should test the output of getNum
.
Upvotes: 0