python 用户输入5个偶数并打印最大的

python user to enter 5 even numbers and print the largest

问题是: 要求用户输入 5 个偶数然后打印最大的

和 python 一直给我这个错误 idk 为什么(unindent 不匹配任何外部缩进级别)我在 Mac

上使用 python.3 IDLE
def main():

     num1 = int(input ("First number"))
     num2 = int(input ("Second number"))
     num3 = int(input ("Third number"))
     num4 = int(input ("Fourth number"))
     num5 = int(input ("Fifth number"))
     n = max (num1, num2, num3, num4, num5)

    if n % 2 and n == 0 :
        print ("The largest number is:", max)
    else:
        print ("we dont take odd numbers here")
main()

在 VScode 中似乎是第 3 到 8 行的意图,所以 if-statement 没有起作用。我做了一个小改动并添加了 print(f"") 并对代码进行了一些调整,因此它现在可以工作了。另外你打印出来的max是一个取最大数的函数,你设置最大数为n。所以只需打印 n 即可。 if 语句应该是 if n % 2 == 0

def main():

    num1 = int(input ("First number"))
    num2 = int(input ("Second number"))
    num3 = int(input ("Third number"))
    num4 = int(input ("Fourth number"))
    num5 = int(input ("Fifth number"))
    n = max(num1, num2, num3, num4, num5)

    if n % 2 == 0 :
        print (f"The largest number is: {n}")
    else:
        print ("we dont take odd numbers here")
main()

如果你不想使用 print(f"") 这也有效。

def main():

    num1 = int(input ("First number"))
    num2 = int(input ("Second number"))
    num3 = int(input ("Third number"))
    num4 = int(input ("Fourth number"))
    num5 = int(input ("Fifth number"))
    n = max(num1, num2, num3, num4, num5)

    if n % 2 == 0 :
        print ("The largest number is: ", n)
    else:
        print ("we dont take odd numbers here")
main()

如果你想让用户输入偶数,如果是奇数再试一次,你可以使用这个方法。

def main():

    numbers = 1
    listWithNumbers = []

    while numbers < 6:
        print(f"Enter nr{numbers}")
        num = int(input("Enter number: "))
        if num % 2 == 0 or num == 0:
            listWithNumbers.append(num)
            numbers = numbers + 1
        else:
            print("we dont take odd numbers here, try again")

    n = max(listWithNumbers)
    print("The largest number is: ", n)

main()
def main():

     num1 = int(input ("First number"))
     num2 = int(input ("Second number"))
     num3 = int(input ("Third number"))
     num4 = int(input ("Fourth number"))
     num5 = int(input ("Fifth number"))
     n = max (num1, num2, num3, num4, num5)
     if (n % 2) == 0:
         print ("The largest number is:", n)
     else:
         print ("we don't take odd numbers here")
main()