2017-04-19 67 views
-5
print("Welcome to calculator.py") 
print ("your options are :") 
print ("") 
print ("1) Addition") 
print ("2)Subtraction") 
print ("3) Multiplication") 
print ("4)Division") 
print ("Choose your option") 


#this adds 2 numbers given 

def add (a,b): 
print(a, "+" ,b, "=" ,a+b) 

#this subtracts 2 numbers given 
def sub (a,b): 
print(a, "-" ,b, "=" ,b-a) 

#this multiplies 2 numbers given 
def mul(a,b): 
print(a, "*" ,b, "=" ,a*b) 

#this divides 2 numbers given 
def div(a,b): 
def div(a, "/" ,b, "=" ,a/b) 

#NOW THE PROGRAM REALLY STARTS ,AS CODE IS RUN 
loop = 1 
choice = 0 
while loop == 1: 
choice = menu() 

if choice == 1: 
    add (input("Add this: "),input ("to this: ")) 

     elif choice == 2: 
    sub(input("Subtract this: "),input ("from this: ")) 


    elif choice == 3: 
    mul (input("Multiply this: "),input ("by this: ")) 

    elif choice == 4: 
    div(input("Divide this: "),input ("by this: ")) 

    elif choice ==5: 
     loop = 0 

     print ("Thank you for using calculator.py") 

這是我的代碼和錯誤是: 打印(一, 「+」,B, 「=」,A + B) ^ IndentationError:預期的縮進塊這是什麼indentationError?

過程與出口完成代碼1 和許多其他人都可以幫助我找到所有的錯誤? 任何人都可以幫我嗎?

+5

你需要(後'DEF)縮進:'和在其他地方(基本上總是後一個冒號':'),你也需要在一些地方解除縮進 –

回答

0

python塊由縮進定義。定義函數後,你應該有一個縮進(tab)。

0

幾乎所有的縮進是錯誤的,不只是一個地方:

print("Welcome to calculator.py") 
print("your options are :") 
print("") 
print("1) Addition") 
print("2)Subtraction") 
print("3) Multiplication") 
print("4)Division") 
print("Choose your option") 


#this adds 2 numbers given 

def add(a,b): 
    print(a, "+", b, "=" , a+b) 

#this subtracts 2 numbers given 
def sub(a,b): 
    print(a, "-", b, "=" , b-a) 

#this multiplies 2 numbers given 
def mul(a,b): 
    print(a, "*", b, "=" , a*b) 

#this divides 2 numbers given 
def div(a,b): 
    print(a, "/", b, "=", a/b) 

#NOW THE PROGRAM REALLY STARTS, AS CODE IS RUN 
loop = 1 
choice = 0 
while loop == 1: 
    choice = menu() 

    if choice == 1: 
     add(input("Add this: "), input("to this: ")) 
    elif choice == 2: 
     sub(input("Subtract this: "), input("from this: ")) 
    elif choice == 3: 
     mul(input("Multiply this: "), input("by this: ")) 
    elif choice == 4: 
     div(input("Divide this: "), input("by this: ")) 
    elif choice ==5: 
     loop = 0 

print ("Thank you for using calculator.py") 
0

在Python中,有identation規則,知道程序應該如何執行命令。這是python在函數,條件語句或for,while語句開始和結束時識別的方式。在你的情況,例如,elif的說法應該是在同一「級別」爲您最初的if語句開始:

print("Welcome to calculator.py") 
print ("your options are :") 
print ("") 
print ("1) Addition") 
print ("2)Subtraction") 
print ("3) Multiplication") 
print ("4)Division") 
print ("Choose your option") 


#this adds 2 numbers given 

def add (a,b): 
    print(a, "+" ,b, "=" ,a+b) 

#this subtracts 2 numbers given 
def sub (a,b): 
    print(a, "-" ,b, "=" ,b-a) 

#this multiplies 2 numbers given 
def mul(a,b): 
    print(a, "*" ,b, "=" ,a*b) 

#this divides 2 numbers given 
def div(a,b): 
def div(a, "/" ,b, "=" ,a/b) 

#NOW THE PROGRAM REALLY STARTS ,AS CODE IS RUN 
loop = 1 
choice = 0 
while loop == 1: 
    choice = menu() 

if choice == 1: 
    add (input("Add this: "),input ("to this: ")) 

elif choice == 2: 
    sub(input("Subtract this: "),input ("from this: ")) 


elif choice == 3: 
    mul (input("Multiply this: "),input ("by this: ")) 

elif choice == 4: 
    div(input("Divide this: "),input ("by this: ")) 

elif choice ==5: 
    loop = 0