0: if dage <= 1: pr" /> 0: if dage <= 1: pr" /> 0: if dage <= 1: pr"/>

conditional statements causes syntax errors in python codes

59 views Asked by At
import traceback

def calculator():
    
    # Get dog age
    age = input("Input dog years: ")

    try:
        # Cast to float
        dage = float(age)
        if dage > 0:
            if dage <= 1:
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 15) ) + " in human years") 
            elif 1 < dage <= 2:
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 12) ) + " in human years")
            elif 2 < dage <= 3 :
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 9.3) ) + " in human years")
            elif 3 < dage <= 4 : 
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 8) ) + " in human years")
            elif 4 < dage <= 5 : 
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 7.2) ) + " in human years")   
            elif 5 < dage : 
                print ("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0)) + " in human years")
        else:
             print ("your input should be a positive number")
      except:
        print(age, "is an invalid age.")
        print(traceback.format_exc())
    
calculator() # This line calls the calculator function

this code calculates the age of a dog in human years but when it was executed, there was an error in the 24th line (else:)

3

There are 3 answers

0
Prins On

Solution to your issue: Your code is missing a closing bracket for str() function in the print statement for your last elif. Here is the correct statement:

elif 5 < dage :
  print ("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0))) + " in human years")

Improvements: You can also use f-strings to improve readability. See this tutorial for more details. Also, you can simplify the conditions in your elif statements as the lower bound is not needed.

Here is the code using f-strings and with some improvements:

def calculator():
    age = input('Input dog years: ')

    try:
        dage = float(age)
        
        if dage > 0:
            msg = f'The given dog age {dage} is '
            if dage <= 1:
                msg += f'{dage * 15:.2f}' 
            elif dage <= 2:
                msg += f'{dage * 12:.2f}'
            elif dage <= 3 :
                msg += f'{dage * 9.3:.2f}'
            elif dage <= 4: 
                msg += f'{dage * 8:.2f}'
            elif dage <= 5 : 
                msg += f'{dage * 7.2:.2f}'
            else: 
                msg += f'{36 + 7 * (dage - 5.0):.2f}'
            msg +=  ' in human years'
            print(msg)
        else:
             print('your input should be a positive number')
    except:
        print(f'{age} is an invalid age.')

calculator()
0
Shisui Otsutsuki On

There is a missing parenthesis in

elif 5 < dage : 
            print("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0)) + " in human years")

Add one more parenthesis at the end of print statement

0
OCP30pt1c1l1l43-X1z17 On

You were missing a bracket on the last print in the else clause.

This is the correct code:

import traceback

def calculator():
    
    # Get dog age
    age = input("Input dog years: ")

    try:
        # Cast to float
        dage = float(age)
        if dage > 0:
            if dage <= 1:
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 15) ) + " in human years") 
            elif 1 < dage <= 2:
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 12) ) + " in human years")
            elif 2 < dage <= 3 :
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 9.3) ) + " in human years")
            elif 3 < dage <= 4 : 
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 8) ) + " in human years")
            elif 4 < dage <= 5 : 
                print ("The given dog age " + str(dage) + " is " + str(round(dage * 7.2) ) + " in human years")   
            elif 5 < dage : 
                print ("The given dog age " +  str(dage) + " is " + str(36 + 7 * (round(dage - 5.0)) + " in human years"))
        else:
            print ("your input should be a positive number")
    except:
        print(age, "is an invalid age.")
        print(traceback.format_exc())
    
calculator() # This line calls the calculator function