2016-10-27 65 views
-1

我试图找到最少的数字,您可以输入尽可能多的数字,当您输入-1时,它会中断。寻找最小编号

a=int(input("What is your number of choice? ")) 
b=int(input("What is your number of choice? ")) 
c=int(input("What is your number of choice? ")) 
d=int(input("What is your number of choice? ")) 
smallest=a 

if (b < smallest): 
    smallest=b 
    while(True): 
    smallest =int(input("What is your number of choice? ")) 

    if (smallest == -1): 
     break 
if (c < smallest): 
    smallest=c 
    while(True): 
    smallest =int(input("What is your number of choice? ")) 

    if (smallest == -1): 
     break 
if (d < smallest): 
    smallest=d 
    while(True): 
    smallest =int(input("What is your number of choice? ")) 

    if (smallest == -1): 
     break 
print(smallest, " is smallest of the numbers you chose") 

我找不出如何解决它。

+5

你没有使用'min'功能的原因? –

+0

a' - '字符不是整数 – smoggers

+0

如果对所有输入使用函数int(),如何在函数min中获得此异常? –

回答

1

您需要设置一个循环以允许用户输入多个值。跟踪这些值可以用列表来完成,每个输入追加到它,如下所示:

all_inputs = [] 
val = int(input('Enter a number (or -1 to finish):')) 

while val != -1: 
    all_inputs.append(val) 
    val = int(input('Enter a number (or -1 to finish):')) 

print(str(min(all_inputs)) + ' is the smallest of your numbers') 

min可以有效地找到最小输入值。

+0

非常感谢您的帮助。我非常感谢,因为我一直在努力奋斗这么久。 –

+0

不客气!但要小心 - 如果有人输入任何数字,它就会中断! – asongtoruin

+0

我一定会记住这一点。 –

0

如果c小于bba小你永远不会因为你进入以前,而bucle达到c<smallest代码。

此外您还需要正确缩进您的代码。

if (b < smallest): 
    smallest=b 
    while(True): 
     smallest =int(input("What is your number of choice? ")) 

     if (smallest == -1): 
      break 

最后中断退出方法不执行las打印行。