Gypsy
Gypsy

Reputation: 113

Syntax Error, Beginner-Level Python 2.7 Code

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)   

Upvotes: 1

Views: 294

Answers (3)

E. Ducateme
E. Ducateme

Reputation: 4248

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)

Upvotes: 1

Joe Iddon
Joe Iddon

Reputation: 20414

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)

Upvotes: 0

gxmad
gxmad

Reputation: 2210

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

Upvotes: 0

Related Questions