0

当我输入 [-2,-2,-5,6,0] 序列时遇到问题,它会打印 -2 而不是 6 问题,以打破任何其他解决方案?

 z = []
    a = 0
    max = 0
    while True:
        b = int(input("Enter a Number ").strip())
        z.append(b)
        if b == 0:
            break
    while a < len(z) :
        if z[a] > z[a+1]:
           max = z[a]
           break
        a += 1
    print(f"the max number is {max} his appearence  is :{z.count(max)}")
4

1 回答 1

0

避免使用诸如“max”之类的关键字作为变量。此外,如评论中所述,请检查您的 while 循环逻辑。如果您可以使用函数/方法,这是您的问题的解决方案。

z = []
while True:
    b = int(input("Enter a Number ").strip())
    z.append(b)
    if b == 0:
        break
max_value = max(z)
index_of_max = z.index(max(z))
print(f"the max number is {max_value} his appearence  is :{index_of_max}")
于 2021-11-14T08:36:35.333 回答