我最近参加了一门 Python 课程,我正在做的练习希望我找到最大和最小的数字。如果我输入一个“字符串”,那么它会提示“无效输入”。这是我到目前为止所得到的,但我收到了回溯错误:
Traceback (most recent call last):
File "FindingSmallestLargestNum.py", line 15, in <module>
if num > largest:
TypeError: '>' not supported between instances of 'float' and
'NoneType'
Run Code Online (Sandbox Code Playgroud)
这是我的代码行:
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done": break
try:
num = float(num)
except:
print("Invalid input")
continue
if smallest is None:
smallest = num
if num > largest:
largest = num
elif num < smallest:
smallest = num
print("Maximum is",int(largest))
print("Minimum …Run Code Online (Sandbox Code Playgroud)