gpt4 book ai didi

python - 不使用 max() 打印列表中的最大数字

转载 作者:行者123 更新时间:2023-11-30 22:38:45 25 4
gpt4 key购买 nike

当用户输入无效值存储到“number”变量中时,我无法防止脚本崩溃。

程序用途:允许用户输入整数,将它们存储到列表中,然后查找并打印最大的数字。不允许在输入中使用字符串或 float 。

功能:

  1. getNum(prompt) - 测试输入值并将其转换为整数并返回 y。
  2. swap(A, x, y) - 用于对数据进行排序的简单交换例程函数。 (当前未使用)
  3. sortNum(A) - 排序并使用列表中的交换例程。
  4. maxIt(numbers) - 查找列表中最大的数字。 (使用中),如果我使用这个 max 函数,我认为我什至不需要对列表进行排序。
  5. 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

在 getNum/numInput 中输入 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 >: 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'

最佳答案

好吧,问题是当数字无效时,您的函数 getNum 返回 None,请尝试以下快速修复:

if numInput is not None:
numbers.append(numInput)

但我也建议您阅读:

Why is "except: pass" a bad programming practice?

并尽量避免代码中出现空的 except 子句!

关于python - 不使用 max() 打印列表中的最大数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43431588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com