当用户输入无效值存储到“数字”变量时,我无法保护脚本不会崩溃。
程序的目的:允许用户输入整数,将它们存储到列表中,然后查找并打印最高编号。输入中不允许使用字符串或浮点数。
功能
getNum(prompt)
- 测试并将输入的值转换为整数并返回y。swap(A, x, y)
- 用于对数据进行排序的简单交换例程函数。 (目前尚未使用)sortNum(A)
- 排序并使用列表中的Swap例程。maxIt(numbers)
- 查找列表中的最高编号。 (使用中),如果我使用这个最大功能,我认为我甚至不需要对列表进行排序。main()
代码:
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()
当getNum的测试没有失败时的当前输出(str或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
将str或float输入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'
答案 0 :(得分:2)
好吧,问题是你的函数getNum
在数字无效时返回None,请尝试以下方法进行快速修复:
if numInput is not None:
numbers.append(numInput)
但我也建议您阅读:
Why is "except: pass" a bad programming practice?
并尝试避免代码中的空except子句!
答案 1 :(得分:1)
问题是你的try..except
在特殊情况下没有返回任何内容(返回None
):
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
您可以通过将输入置于循环中来避免这种情况:
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")
答案 2 :(得分:0)
问题位于函数getNum
中。实际上,当您在输入中键入一些文本时,函数getNum
会出现异常,因为您尝试将非数字str
强制转换为int
,然后打印消息并且函数返回...... None
。 None
存储在numInput中,然后添加到列表中。你知道接下来的事情。
为了使这项工作,你应该测试getNum
的输出。
答案 3 :(得分:0)
您的getNum
返回int或None(打印消息后)。 main
即使无,也无条件地附加返回值。因此比较信息。变化
numbers.append(numInput)
到
if isinstance(numInput, int):
numbers.append(numInput)