-2

The question is : Ask the user to enter 5 even numbers then print the largest

and python keep giving me this error idk why (unindent does not match any outer indentation level) I am using python.3 IDLE on Mac

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()
Terry Jan Reedy
  • 18,414
  • 3
  • 40
  • 52
Lava
  • 9
  • 3
  • [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) Include it as a [formatted code block](/help/formatting) instead of an image. – Pranav Hosangadi Feb 25 '22 at 21:48
  • _python keeps giving me this error and I don't know why (unindent does not match any outer indentation level)_: Could it be because (idk) that line is unindented to a level that does not match any outer indentation level? Indentation matters in python, and any basic tutorial should cover this. With two lines that belong to the same block, you can't have one line indented to four spaces, and then the next line indented to three spaces – Pranav Hosangadi Feb 25 '22 at 21:52
  • Try https://stackoverflow.com/q/23294658/13552470 – Red Feb 25 '22 at 22:07
  • Once you fix that, you will notice that your program accepts even numbers, because you do nothing to prevent it from accepting even numbers. See [Asking the user for input until they give a valid response](//stackoverflow.com/q/23294658/843953). Additionally, you print `max`, which is a _function_ that calculates the max. The result of that function is in `n`, which you've already used to check `n % 2`, so _print `n`, not `max`_. Finally, why do you check for `n == 0` before you print it? – Pranav Hosangadi Feb 25 '22 at 22:08
  • right, I removed n==0 but I didn't get what you mean about max because I print n and used max to get the highest number? – Lava Feb 25 '22 at 22:20

2 Answers2

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 ("The largest number is:", n)
     else:
         print ("we don't take odd numbers here")
main()
coding4fun
  • 3,485
  • 1
  • 16
  • 28
-1

In VScode it seems like the intendeation was of on rows 3 to 8 so the if-statement didnt work. I made a small change and added the print(f"") and made a few adjustments to the code so it whould work now. Also you print out max witch is a function to get the higest number, and you set the highest number to n. So just print n instead. The if statement should be 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()

If you dont want to use print(f"") this works too.

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()

If you want to make the user type in a even number and try again if its odd you can use this method.

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()
  • Thank you! but I deleted ==0 because I only want the user to enter even number but the program still accept odd numbers? `First number1 Second number2 Third number3 Fourth number4 Fifth number5 The largest number is: 5 ` – Lava Feb 25 '22 at 22:33
  • Do you want the user to only be able to enter even numbers and have them try again if they dont? So you want a kind of validator? – DancingAlfred Feb 25 '22 at 22:50
  • yes sir, only even numbers if not the program should show error massage and let the user to try again. – Lava Feb 25 '22 at 22:58
  • Ok, i made a loop for that. Doesnt work if the user types in a negative number,string or float. Is this more like what you wanted? – DancingAlfred Feb 25 '22 at 23:19
  • I works but when I entered 4 even numbers and one odd the program ignores it! I don't know if its some sort of python errors or there is something wrong in the codes. – Lava Feb 25 '22 at 23:23
  • Hmm, doesnt it ask you to try again when you enter a odd number? – DancingAlfred Feb 25 '22 at 23:25
  • I edited my post and that 3rd code works for me. But I am running VScode and on Windows but that should not matter – DancingAlfred Feb 25 '22 at 23:31