2017-04-19 37 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