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'