Python 如何修复0输出I';我参加这个项目了吗?

Python 如何修复0输出I';我参加这个项目了吗?,python,Python,我可能会让这个程序比它需要的更复杂。但我不得不限制自己,因为这是学习课程的一部分。在大多数情况下,这似乎是可行的,尽管我似乎总是得到一个“最大”整数的0输出。有时候,当输入类似的数字序列时,比如555,我也会看到5,0,0返回 我只是想让程序拉动排列数字序列。我确信这在很多方面都是可能的,但我想看看我的这种方式是否可以优化。非常感谢您的投入 first = int(input("Enter the first number: ")) second = int(input(&q

我可能会让这个程序比它需要的更复杂。但我不得不限制自己,因为这是学习课程的一部分。在大多数情况下,这似乎是可行的,尽管我似乎总是得到一个“最大”整数的0输出。有时候,当输入类似的数字序列时,比如555,我也会看到5,0,0返回

我只是想让程序拉动排列数字序列。我确信这在很多方面都是可能的,但我想看看我的这种方式是否可以优化。非常感谢您的投入

first = int(input("Enter the first number: "))
second = int(input("Enter the second number: "))
third = int(input("Enter the third number: ")) 
small = 0
middle = 0
large = 0 
if first < third and first < second:
    small = first
elif second < third and second < first:
    small = second
else:
    small = third 
if first < second and first < third:
    middle = first
    if second > first and second < third:
        middle = second
    else:
        middle = third 
elif first > second and first > third:
    large = first
    if second > first and second > third:
        large = second
    else:
        large = third
print("The numbers in accending order are: ", small, middle, large)
first=int(输入(“输入第一个数字:”)
second=int(输入(“输入第二个数字:”)
第三个=整数(输入(“输入第三个数字:”)
小=0
中间=0
大=0
如果第一次<第三次且第一次<第二次:
小=第一
elif second第一次,第二次<第三次:
中间=秒
其他:
中间=第三
elif第一次>第二次和第一次>第三次:
大=第一
如果第二次>第一次和第二次>第三次:
大=秒
其他:
大=第三
打印(“按顺序排列的数字为:”、小、中、大)

问题在于,如果实际执行了
语句,则没有任何
,因此
小、中、大的
值不会改变(它们保持在初始值,
0

这样做的原因是,您正在检查值是否大于或小于彼此,而实际上它们是相等的(返回
False
)。由于没有执行
if
语句,因此只执行
else
语句(因此
5,0,0
中的
5

尝试这样做:

# NOTE: the change is the <= and >= rather than simply < or >
if first <= third and first <= second:
    small = first
elif second <= third and second <= first:
    small = second
else:
    small = third

# NOTE: I also made a few changes to the code below

# Reason: Earlier middle wasn't being set at all if first >= second and first <= third was
# false, a similar reason goes for the large value not changing

# I also changed the definition of middle to include the fact that first can be more than
# second and less than third or vice versa (more than third and less than second)
if (first >= second and first <= third) or (first <= second and first >= third):
    middle = first
elif (second >= first and second <= third) or (second <= first and second >= third):
    middle = second
else:
    middle = third 
if first >= second and first >= third:
    large = first
elif second >= first and second >= third:
    large = second
else:
    large = third
#注意:更改是=而不是简单的

如果第一个指定的
小的第一部分是正确的,除了只检查
,如果两个或两个以上的数字相等,这将不起作用。因此,我将比较运算符更改为
=
,并以相同的样式重写了其他两个部分。现在看起来是这样的(这是可行的):

first=int(输入(“输入第一个数字:”)
second=int(输入(“输入第二个数字:”)
第三个=整数(输入(“输入第三个数字:”)
小=0
中间=0
大=0
如果第一个=第三个:
大=秒
其他:
大=第三
打印(“按顺序排列的数字为:”、小、中、大)

原样代码尝试查找最小、中间和最大的代码,而不考虑前面的步骤。记住以前的检查要简单得多,例如:

first=int(输入(“输入第一个数字:”)
second=int(输入(“输入第二个数字:”)
第三个=整数(输入(“输入第三个数字:”)
小=0
中间=0
大=0

if first您的第二个
if
语句与第一个
if
语句相同。这是故意的吗?您只需在代码中eah分支之后打印一些独特的内容,就可以自己调试它。或者使用debugger.FWIW,您可以将所有这些压缩到
打印(排序((第一、第二、第三))
…。这也会有帮助,您可能只需要将所有运算符更改为相同的选项:
重新查看您的代码。有些事情根本上是错的。您将始终指定
middle
large
,因为它们是
if/elif
的一部分。因此,您永远不会同时指定这两个。考虑到这些限制和我的基本理解,这听起来不像你想让事情变得比它需要的更复杂。到目前为止,输入在一定程度上有所帮助。好的,我想我现在已经修复了它,很抱歉给您带来了不便。如果您知道,例如,
small=first
middle
second
third
中的较小者,这不是会简单得多吗。
first = int(input("Enter the first number: "))
second = int(input("Enter the second number: "))
third = int(input("Enter the third number: "))
small = 0
middle = 0
large = 0

if first <= third and first <= second:
    small = first
elif second <= third and second <= first:
    small = second
else:
    small = third

if first <= second and first >= third or first >= second and first <= third:
    middle = first
elif second >= first and second <= third or second <= first and second >= third:
    middle = second
else:
    middle = third

if first >= second and first >= third:
    large = first
elif second >= first and second >= third:
    large = second
else:
    large = third
print("The numbers in accending order are: ", small, middle, large)