1

Extreme beginner at code here. Going through 'Introduction to Computation and Programming Using Python' by John V. Guttag. One of the first problems is:

"Write a program that examines three variables—x, y, and z— and prints the largest odd number among them. If none of them are odd, it should print a message to that effect."

The code I came up with has a Syntax Error on lines 5, 7, and 9. As I'm a complete beginner, I can't figure it out. Advice is welcome!

FirstNumber=int(input("Enter First Number:"))  
SecondNumber=int(input("Enter Second Number:"))  
ThirdNumber=int(input("Enter Third Number:"))  

if (FirstNumber%2!=0:) and (FirstNumber > SecondNumber and FirstNumber > ThirdNumber)  
  print 'First Number is largest odd -> '+str(FirstNumber)  
elif (SecondNumber%2!=0:) and (SecondNumber > FirstNumber and SecondNumber > ThirdNumber)  
  print 'Second Number is largest odd -> '+str(SecondNumber)  
elif (ThirdNumber%2!=0:) and (ThirdNumber > FirstNumber and ThirdNumber > SecondNumber)  
  print 'Third Number is largest odd -> '+str(ThirdNumber)  
else:   
  print 'None are odd -> '+str(FirstNumber), str(SecondNumber), str(ThirdNumber)   
6
  • 2
    if needs ':' at end. Go get a better IDE, they will highlight the errors. I use PyCharm Commented Jan 15, 2018 at 19:55
  • That's if and elif statements all need a : at the end of the line after the condition (you did it right for else). Commented Jan 15, 2018 at 19:58
  • Komodo edit will work as well. BTW welcome to SO. Hoora for finishing the tour and nice typo question ;-) Review finished. Enjoy SO! Commented Jan 15, 2018 at 20:03
  • @ZF007 Thanks, I'm still trying to find the right program to use that feels most convenient, but I'm currently at work and using repl.it/repls, desperate times lol. I'll definitely look that one up later! Commented Jan 15, 2018 at 20:22
  • @StacyD.. you're 200% sure you want to share all your code/work with repl.it?.. unless its your boss? Commented Jan 15, 2018 at 20:47

3 Answers 3

1

As noted in the comments, your code is missing the colon at the end of each of your if and elif statements. Also, your code has extraneous colons within the first conditional test (ie. (FirstNumber%2!=0:).

Also, as you mention that you are a beginner, a few suggestions on simplifying the code... or improving the readability...

firstNum = int(input("Enter First Number: "))  
secondNum = int(input("Enter Second Number: "))  
thirdNum = int(input("Enter Third Number: "))  


# In this case, we don't need the parenthesis. (Sometimes parens are
# required to ensure that the logic works correctly, but in this case,
# we don't.) Python short circuits, meaning it will stop the If 
# statement mid-way as soon as a conditional statement equivocates to False.

if firstNum % 2 != 0 and firstNum > secondNum and firstNum > thirdNum:  
    print 'First Number is largest odd -> ' + str(firsthirdNum)  
elif secondNum % 2 != 0 and secondNum > firstNum and secondNum > thirdNum:  
    print 'Second Number is largest odd -> ' + str(secondNum)  
elif thirdNum % 2 != 0 and thirdNum > firstNum and thirdNum > secondNum: 
    print 'Third Number is largest odd -> ' + str(thirdNum)  
else:   
    print 'None are odd -> ' + str(firstNum), str(secondNum), str(thirdNum)
Sign up to request clarification or add additional context in comments.

Comments

0

You need colons (:) at the end of each condition in your if-statement (not at then end of each composing part). It is just syntax that you need to know; nothing special.

Two more unnecessary things are that you should add some spaces to make the operations clearer, and can remove the != 0 checks as any integer which isn't 0 evaluates to True so will achieve the same effect. Finally, the brackets are not needed.

So here's the corrected code:

FirstNumber  = int(input("Enter First Number:"))  
SecondNumber = int(input("Enter Second Number:"))  
ThirdNumber  = int(input("Enter Third Number:"))  

if FirstNumber % 2    and FirstNumber>SecondNumber and FirstNumber>ThirdNumber:
  print 'First Number is largest odd -> '+str(FirstNumber)  
elif SecondNumber % 2 and SecondNumber>FirstNumber and SecondNumber>ThirdNumber:
  print 'Second Number is largest odd -> '+str(SecondNumber)  
elif ThirdNumber % 2  and ThirdNumber>FirstNumber  and ThirdNumber>SecondNumber:
  print 'Third Number is largest odd -> ' + str(ThirdNumber)  
else:   
  print 'None are odd -> ' + str(FirstNumber), str(SecondNumber), str(ThirdNumber)

2 Comments

Thanks so much! There's so many little things I'm still trying to wrap my head around, and I wanted to get this one down before I moved on to the next one. Placement of brackets still confuses me, where they're needed and where they're unnecessary. I guess that'll come with experience!
@StacyD Yep! They are only really necessary in function calls and for mathematical arithmetic. You do occasionally need them if doing a compound boolean logic statement (as in these if-statements) but as all your "gates" were AND, they are unnecessary as evaluation ordering is irrelevant.
0

You forgot : In python, if, elif and else always end with ':' Also when you create a function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.