7 回答
TA贡献1829条经验 获得超7个赞
您在打印输出时遗漏了一些语句。
以下代码对我有用。
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
#print(num)
num = int(num)
if largest is None or largest < num:
largest = num
elif smallest is None or smallest > num :
smallest = num
except ValueError:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimum is", smallest)
已验证并成功运行。
TA贡献1799条经验 获得超8个赞
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try:
num = int(num)
except:
print("Invalid input")
continue
if largest==None or num > largest:
largest = num
elif smallest==None or smallest > num:
smallest = num
print("Maximum is", largest)
print("Minimum is", smallest)
TA贡献1802条经验 获得超10个赞
如果包含 elif 语句,则如果 if 语句中的条件为真,则不会检查它们,因此如果我有“elif smallest...”,则永远不会检查结果为“Minimus is none”。
largest = None
smallest = None
while True:
enter_num = input('Enter a number ')
if enter_num == 'done':
break
try:
num = int(enter_num)
if largest is None or largest < num:
largest = num
if smallest is None or smallest > num:
smallest = num
except:
print('Invalid input')
continue
print('Maximum is', largest)
print('Minimum is', smallest)
TA贡献1843条经验 获得超7个赞
试试这个代码,稍后谢谢我
largest = None
smallest = None
while True:
val = input("Enter a number: ")
if val == "done":
break
try:
val = int(val)
if largest is None or val > largest:
largest = val
elif smallest is None or smallest > val:
smallest = val
except:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimum is", smallest)
TA贡献1886条经验 获得超2个赞
largest = 0
smallest = 100
while True:
num = input('Enter a number: ')
if num == 'done' :
break
try:
l1 = int(num)
except:
print('Invalid Input')
continue
#print(l1)
if l1 > largest:
l2 = l1
largest = l2
elif l1 < smallest:
l3 = l1
smallest = l3
print('Maximum', largest)
print('Minimum', smallest)
这是我唯一能解决的问题。无法弄清楚如何将最大和最小值保持为 none
TA贡献1906条经验 获得超3个赞
largest = None
smallest = None
while True:
try:
num = input("Enter a number: ")
if num == "done":
break
#print(num)
num=int(num)
if largest is None or largest < num:
largest=num
elif smallest is None or smallest > num:
smallest=num
except:
print("Invalid input")
continue
print("Maximum is", largest)
print("Minimum is", smallest)
TA贡献1818条经验 获得超11个赞
largest = None
smallest = None
while True:
num = input("Enter a number: ")
if num == "done":
break
try :
n = int(num)
except :
print("Invalid input")
continue
if largest is None or largest < n:
largest = n
elif smallest is None or smallest > n:
smallest = n
print("Maximum is", largest)
print("Minimum is", smallest)
添加回答
举报