简体   繁体   中英

Why is my if loop throwing up a syntax error?

I have started making a calculator in python and it is throwing up a pyflake syntax error that I can't get rid of.

This is what I have done so far:

import time
import math 
from clear_screen import clear

def add(x, y):
    return x + y
    
def subtract(x, y):
  return x - y

def multiply(x, y):
  return x * y

def divide(x, y):
  return x / y

def power(x, y):
  return x ** y
    
while 0 == 0:

  print ("======================")
  print ("calculator.exe")
  print ("======================")
  print ("")

  time.sleep(0.3)

  print ("1) Add ")
  print ("2) subtract ")
  print ("3) Multiply ")
  print ("4) Devide ")
  print ("5) Square Root ")
  print ("6) Power")
  print ("======================")
  x = input ("select operator ")
  if x == '5':
    num1 = int(input ("Type number "))
  elif x == '6':
    num1 = int(input ("Type number"))
    num2 = int(input ("Type Power"))
  else:
    num1 = int(input ("Type first number "))
    num2 = int(input ("Type Second number "))
  print ("======================")

  if x == '1':
    print (num1 ,"+",num2, "=" , add(num1,num2))
  
  elif x == '2':
    print (num1 ,"-",num2, "=" , subtract(num1,num2))
 
  elif x == '3':
    print (num1 ,"*",num2, "=" , multiply(num1,num2))
  
  elif x == '4':
    print (num1 ,"/",num2, "=" , divide(num1,num2))
  
  elif x == '5':
    print ("√", num1, "=" ,(math.sqrt(num1)))   
  
  elif x == "6":
    print (num1,"^",num2, "=" , power(num1,num2))  

  else:
    print ("Opperator not found")
  print ("======================")

  time.sleep(3)
  clear()

(Original code available at https://repl.it/join/mqrgvlcf-rosmonautical ) (I don't care how inefficient it is, its still mine)

You are missing a ) at the end of the previous statement:

if x == '1':
    print (num1 ,"+",num2, "=" , add(num1,num2))
  
elif x == '2':
    print (num1 ,"-",num2, "=" , subtract(num1,num2))
 
elif x == '3':
    print (num1 ,"*",num2, "=" , multiply(num1,num2))
  
elif x == '4':
    print (num1 ,"/",num2, "=" , divide(num1,num2))
  
elif x == '5':
    print ("√", num1, "=" ,(math.sqrt(num1))) # Last ")" was missing here
  
elif x == "6":
    print (num1,"^",num2, "=" , power(num1,num2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM