Click here to Skip to main content
15,887,485 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There seems to be a syntax error in the "elif choice == "%":" statement. Please help me out.

What I have tried:

Python
  1  def add(a, b):
  2      return a + b
  3  
  4  # This function subtracts two numbers
  5  def subtract(a, b):
  6      return a - b
  7  
  8  # This function multiplies two numbers
  9  def multiply(a, b):
 10      return a * b
 11  
 12  # This function divides two numbers
 13  def divide(a, b):
 14      return a / b
 15      
 16  # This function displays the power of two numbers
 17  def power(a, b):
 18      return a ^ b
 19  
 20  # This function displays the remainder of two numbers
 21  def remainder(a, b):
 22      return a % b
 23  
 24  #-------------------------------------
 25  #TODO: Write the select_op(choice) function here
 26  #This function sould cover Task 1 (Section 2) and Task 3
 27  
 28  def select_op(choice):
 29      if (choice == "#"):
 30         return -1
 31    
 32      elif (choice in('+','-','*','/','^','%','#','$')):
 33          while true:
 34              firstnum=input("Enter the first number:")
 35              print(firstnum)
 36              try: 
 37                  if firstnum == '0$':
 38                   return 0
 39                  else:
 40                          num1=float(firstnum)
 41              except:
 42                              print("Not a valid number,please enter again")
 43                              continue
 44              secondnum=input("Enter the second number: ")
 45              print(secondnum)
 46              try:
 47                  if secondnum=='0$':
 48                   return 0
 49                  elif secondnum=='#':
 50                    return -1
 51                  else:
 52                       num2=float(secondnum)
 53                  break
 54              except:
 55               print("Not a valid number,please enter again")
 56              continue
 57          if choice =="+":
 58      
 59             print(num1,"+", num2,"=",add(num1,num2))
 60           
 61          elif choice == '-':
 62             print(num1,"-", num2,"=",subtract(num1,num2)) 
 63             
 64          elif choice == '*':
 65             print(num1,"*", num2,"=",multiply(num1,num2)) 
 66             
 67          elif choice == '/':
 68             if num2==0:
 69                 print("float division by zero")
 70                 print(num1,"/",num2,"=", divide(num1,num2))
 71             
 72          elif choice == '^':
 73              num1=int(num1)
 74              num2=int(num2)
 75          print(num1,"^", num2,"=",power(num1,num2)) 
 76          
 77             
 78          elif choice == "%":
 79          print(num1,"%", num2,"=",remainder(num1,num2)) 
 80          elif choice == '$':
 81          return select_op()  
 82          else:
 83          print("Unrecognized operation")
 84  
 85  #End the select_op(choice) function here
 86  #-------------------------------------
 87  #This is the main loop. It covers Task 1 (Section 1)
 88  #YOU DO NOT NEED TO CHANGE ANYTHING BELOW THIS LINE
 89  while True:
 90    print("Select operation.")
 91    print("1.Add      : + ")
 92    print("2.Subtract : - ")
 93    print("3.Multiply : * ")
 94    print("4.Divide   : / ")
 95    print("5.Power    : ^ ")
 96    print("6.Remainder: % ")
 97    print("7.Terminate: # ")
 98    print("8.Reset    : $ ")
 99    
100  
101    # take input from the user
102    choice = input("Enter choice(+,-,*,/,^,%,#,$): ")
103    print(choice)
104    if(select_op(choice) == -1):
105      #program ends here
106      print("Done. Terminating")
107      exit()
Posted
Updated 12-Aug-23 6:51am
v4
Comments
Richard MacCutchan 12-Aug-23 12:55pm    
See my updated solution.
F Atheeya 12-Aug-23 13:38pm    
Thank you so much , it helped..

Python
print(num1,"^", num2,"=",power(num1,num2))

elif choice == "%":
print(num1,"%", num2,"=",remainder(num1,num2))
elif choice == '$':
return select_op()
else:
print("Unrecognized operation")

Look at the first print statement above, and the statements that follow the two elif and the else clauses: none of them are correctly indented.

[edit]
The problem at line 84 is due to the if statement at line 29, which is followed by an elif at line 32, but does not have a corresponding else statement.
[/edit]
 
Share this answer
 
v2
The major problem with Python, unlike every other language out there, is the indentation every line of code matters! It determines which scope the line of code belongs to. Screw that up and you're scratching your head with weird problems like you have here.

Scope is a concept that determines what variables and functions are accessible to other code. In Python, the current scope isn't very obvious if you don't understand how important indentation is. In other languages, like C/C++, Java, and C#, scope is denoted by curly braces. It becomes very obvious where a scope begins and ends.

In your case, your code is not consistently and properly indented, and hence you have this problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900