【问题标题】:python program for finding greater number among of 4 numbers, using nested ifpython程序用于在4个数字中查找更大的数字,使用嵌套if
【发布时间】:2022-11-12 23:33:57
【问题描述】:

这段代码有什么问题? 当 C 和 D 是较大的数字时,我的 PC 没有显示输出?

a=int(input("ent a no."))
b=int(input("ent a no."))
c=int(input("ent a no."))
d=int(input("ent a no."))

if a>b:
    if a>c:
        if a>d:
            print(" a is greater")
            
            
elif b>a:
    if b>c:
        if b>d:
            print("b is greater")

elif c>a:
    if c>b:
        if c>d:
            print ("c bada hai bc")

else:

    print("d is greater") 

该程序在 A 和 B 变量具有较大数字时显示输出,但在 D 和 C 分别具有较大数字时不显示任何输出?

【问题讨论】:

  • 假设您输入的数字是 1、2、3、2。在这种情况下b大于一个所以第二个小精灵在您的代码中永远不会被考虑。另外,如果输入是 1, 2, 3, 3 你想要什么输出?

标签: python if-statement nested-if


【解决方案1】:

如果c > a,您的最终else 将不会被调用,但如果c < d 也不会打印。这也是非常糟糕的形式,您可以像这样构造它:

if a > b and a > c and a > d:
    print("a is greater"

    .
    .
    .

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2021-02-06
    • 2020-06-22
    • 1970-01-01
    相关资源
    最近更新 更多