Coursera - 适合所有人的 Python - ex 5.2

对于以下问题:

编写一个程序,重复提示用户输入整数,直到用户输入done。

输入“完成”后,打印出最大和最小的数字。

如果用户输入的不是有效数字,则使用 try/except 捕获它并发出适当的消息并忽略该数字。

输入个案:

输入 7、2、bob、10 和 4 并匹配下面的输出。


我的程序没有正确显示答案。这里有什么问题?


while True:

inp = input("Enter number: ")


if inp == "done":

    print("done")

    break

try:

    inp_data = int(inp)

except:

    print("Invalid input")

if smallest is None:

    smallest = inp_data

elif inp_data < smallest:

    smallest = inp_data

elif inp_data > largest:

    largest = inp_data

    

print("largest", largest)

print("smallest", smallest)

输出:

无效输入,最大值为 10,最小值为 2。

http://img.mukewang.com/63b530210001968613120316.jpg

蝴蝶不菲
浏览 129回答 7
7回答

吃鸡游戏

您在打印输出时遗漏了一些语句。以下代码对我有用。largest = Nonesmallest = Nonewhile True:&nbsp; &nbsp; num = input("Enter a number: ")&nbsp; &nbsp; if num == "done":&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; #print(num)&nbsp; &nbsp; &nbsp; &nbsp; num = int(num)&nbsp; &nbsp; &nbsp; &nbsp; if largest is None or largest < num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; largest = num&nbsp; &nbsp; &nbsp; &nbsp; elif smallest is None or smallest > num :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smallest = num&nbsp; &nbsp; except ValueError:&nbsp; &nbsp; &nbsp; &nbsp; print("Invalid input")&nbsp; &nbsp; &nbsp; &nbsp; continueprint("Maximum is", largest)&nbsp; &nbsp;&nbsp;print("Minimum is", smallest)已验证并成功运行。

守着星空守着你

largest = Nonesmallest = Nonewhile True:&nbsp; &nbsp; num = input("Enter a number: ")&nbsp; &nbsp; if num == "done":&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; num = int(num)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print("Invalid input")&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; if largest==None or num > largest:&nbsp; &nbsp; &nbsp; &nbsp; largest = num&nbsp; &nbsp; elif smallest==None or smallest > num:&nbsp; &nbsp; &nbsp; &nbsp; smallest = numprint("Maximum is", largest)print("Minimum is", smallest)

守候你守候我

如果包含 elif 语句,则如果 if 语句中的条件为真,则不会检查它们,因此如果我有“elif smallest...”,则永远不会检查结果为“Minimus is none”。largest = Nonesmallest = Nonewhile True:&nbsp; &nbsp; enter_num = input('Enter a number ')&nbsp; &nbsp; if enter_num == 'done':&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; num = int(enter_num)&nbsp; &nbsp; &nbsp; &nbsp; if largest is None or largest < num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; largest = num&nbsp; &nbsp; &nbsp; &nbsp; if smallest is None or smallest > num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;smallest = num&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print('Invalid input')&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;print('Maximum is', largest)print('Minimum is', smallest)

蓝山帝景

试试这个代码,稍后谢谢我largest = Nonesmallest = Nonewhile True:&nbsp; &nbsp; val = input("Enter a number: ")&nbsp; &nbsp; if val == "done":&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; val = int(val)&nbsp; &nbsp; &nbsp; &nbsp; if largest is None or val > largest:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; largest = val&nbsp; &nbsp; &nbsp; &nbsp; elif smallest is None or smallest > val:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smallest = val&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print("Invalid input")&nbsp; &nbsp; &nbsp; &nbsp; continueprint("Maximum is", largest)print("Minimum is", smallest)

MM们

largest = 0smallest = 100while True:&nbsp; &nbsp; num = input('Enter a number: ')&nbsp; &nbsp; if num == 'done' :&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; l1 = int(num)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print('Invalid Input')&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; #print(l1)&nbsp; &nbsp; if l1 > largest:&nbsp; &nbsp; &nbsp; &nbsp; l2 = l1&nbsp; &nbsp; &nbsp; &nbsp; largest = l2&nbsp; &nbsp; elif l1 < smallest:&nbsp; &nbsp; &nbsp; &nbsp; l3 = l1&nbsp; &nbsp; &nbsp; &nbsp; smallest = l3print('Maximum', largest)print('Minimum', smallest)这是我唯一能解决的问题。无法弄清楚如何将最大和最小值保持为 none

一只名叫tom的猫

largest = Nonesmallest = Nonewhile True:&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; num = input("Enter a number: ")&nbsp; &nbsp; &nbsp; &nbsp; if num == "done":&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; #print(num)&nbsp; &nbsp; &nbsp; &nbsp; num=int(num)&nbsp; &nbsp; if largest is None or largest < num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; largest=num&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; elif smallest is None or smallest > num:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; smallest=num&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; print("Invalid input")&nbsp; &nbsp; &nbsp; &nbsp; continueprint("Maximum is", largest)print("Minimum is", smallest)

慕尼黑8549860

largest = Nonesmallest = Nonewhile True:&nbsp; &nbsp; num = input("Enter a number: ")&nbsp; &nbsp; if num == "done":&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; try :&nbsp; &nbsp; &nbsp; &nbsp; n = int(num)&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; except :&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; print("Invalid input")&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; if largest is None or largest < n:&nbsp; &nbsp; &nbsp; &nbsp; largest = n&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; elif smallest is None or smallest > n:&nbsp; &nbsp; &nbsp; &nbsp; smallest = n&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;print("Maximum is", largest)print("Minimum is", smallest)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python