Python 寻找最小的数字

Python 寻找最小的数字,python,Python,我试图找到一个最小的数字,在这里你可以输入尽可能多的数字,当你输入-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

我试图找到一个最小的数字,在这里你可以输入尽可能多的数字,当你输入-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")
a=int(输入(“您的选择数量是多少?”)
b=int(输入(“您的选择数量是多少?”)
c=int(输入(“您选择的数量是多少?”)
d=int(输入(“您选择的数量是多少?”)
最小的=a
如果(b<最小值):
最小=b
虽然(正确):
最小值=int(输入(“您选择的数量是多少?”)
如果(最小==-1):
打破
如果(c<最小值):
最小=c
虽然(正确):
最小值=int(输入(“您选择的数量是多少?”)
如果(最小==-1):
打破
如果(d<最小值):
最小=d
虽然(正确):
最小值=int(输入(“您选择的数量是多少?”)
如果(最小==-1):
打破
打印(最小,“是您选择的最小数字”)

如果
c
小于
b
并且
b
小于
a
您永远无法到达
c您需要设置一个循环,允许用户输入多个值。可以使用列表跟踪这些值,并将每个输入添加到列表中,如下所示:

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
可以有效地找到最小值输入。

您不使用
min
函数的任何原因?一个“-”字符不是整数如果您使用函数int()进行所有输入,如何在函数min中获得此异常?非常感谢您的帮助。我非常感激,因为我已经为此奋斗了这么久。不客气!不过要小心——如果有人输入数字以外的任何东西,它都会断开!
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')