python计算器程序

时间:2012-12-01 23:19:34

标签: python python-2.7 calculator cal

我使用函数写了一个简单的计算器程序,我不知道这段代码到底是什么错,它显示错误。我做了调试这个的可能步骤,但我做不到。

#!/usr/bin/python

def add():        
        print "Enter the two numbers to Add"
        A=int(raw_input("Enter A:"))
        B=int(raw_input("ENter B:"))
        c = A + B 

def sub():            
        print "Enter the two numbers to Subtract"
        A=int(raw_input("Enter A:"))
        B=int(raw_input("Enter B:"))
        c = A - B

def Mul():
        print "Enter the two numbers to Multiply"
        A=int(raw_input("Enter A:"))
        B=int(raw_input("Enter B:"))
        c = A * B

def Div():   
        print "Enter the two number to Divide"
        A=float(raw_input("Enter A:"))
        B=float(raw_input("Enter B:"))
        c = A / B

print "1: ADDITION"
print "2: SUBTRACTION"
print "3: MULTIPLICATION"
print "4: DIVITION"
print "0: QUIT"

while true:

    CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION")) 

    if CHOICE == "1": 
        print 'ADDING TWO NUMBERS:'
        add(c):

    elif CHOICE == "2":
        print 'SUBTRACTING TWO NUMBERS:'
        sub(c):

    elif CHOICE == "3":
        print 'MULTIPLYING TWO NUMBERS:'
        Mul(c):

    elif CHOICE == "4":
        print "DIVIDEING TWO NUMBERS"
        Div(c):

    elif CHOICE == "0":
        return 0:

    else
        Print "The value Enter value from 1-4"

错误:

  File "cal_fun.py", line 44
    if CHOICE == "1": 
                  ^
SyntaxError: invalid syntax

4 个答案:

答案 0 :(得分:4)

我已尝试用您的代码解决所有问题,其中有很多问题。

syntax错误开始:

# true needed a captial T
while True:   

    # Brackets were mismatched
    CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION")) 

    if CHOICE == "1": 
        print 'ADDING TWO NUMBERS:'
        # Calling a function shouldn't have trailing : 
        add(c)

    elif CHOICE == "2":
        print 'SUBTRACTING TWO NUMBERS'
        # Calling a function shouldn't have trailing :
        sub(c)

    elif CHOICE == "3":
        print 'MULTIPLYING TWO NUMBERS'
        # Calling a function shouldn't have trailing :
        Mul(c)

    elif CHOICE == "4":
        print "DIVIDEING TWO NUMBERS"
        # Calling a function shouldn't have trailing :
        Div(c)

    elif CHOICE == "0":
        # can only return from a function use exit here instead
        exit()

    # else needs a trailing :
    else:
        # No capital P for print
        print "The value Enter value from 1-4"

代码现在没有syntax错误,但仍有许多问题。

  1. 您将c传递给您的函数,c永远不会被初始化,什么是c
  2. 您的函数不接受参数def add(): (即使传递了神秘的c值)
  3. 您的功能不会printreturn它只计算结果。
  4. 您存储CHOICE作为int与字符串进行比较,因此始终执行else大小写并且无法退出循环(无限循环)< / em>的。

  5. 固定代码:

    #!/usr/bin/python
    
    def add():
            print "Enter the two numbers to Add"
            A=int(raw_input("Enter A: "))
            B=int(raw_input("Enter B: "))
            return A + B 
    
    def sub():
            print "Enter the two numbers to Subtract"
            A=int(raw_input("Enter A: "))
            B=int(raw_input("Enter B: "))
            return A - B
    
    def mul():
            print "Enter the two numbers to Multiply"
            A=int(raw_input("Enter A: "))
            B=int(raw_input("Enter B: "))
        return A * B
    
    def div():
            print "Enter the two number to Divide"
            A=float(raw_input("Enter A: "))
            B=float(raw_input("Enter B: "))
            return A / B
    
    print "1: ADDITION"
    print "2: SUBTRACTION"
    print "3: MULTIPLICATION"
    print "4: DIVITION"
    print "0: QUIT"
    
    while True:
    
        CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION ")) 
    
        if CHOICE == 1: 
            print 'ADDING TWO NUMBERS:'
            print add()
    
        elif CHOICE == 2:
            print 'SUBTRACTING TWO NUMBERS'
            print sub()
    
        elif CHOICE == 3:
            print 'MULTIPLYING TWO NUMBERS'
            print mul()
    
        elif CHOICE == 4:
            print "DIVIDEING TWO NUMBERS"
            print div()
    
        elif CHOICE == 0:
            exit()
        else:
            print "The value Enter value from 1-4"
    

    代码现在正常运作。

    <强>输出:

    1: ADDITION
    2: SUBTRACTION
    3: MULTIPLICATION
    4: DIVITION
    0: QUIT
    ENTER THE CORRESPONDING NUMBER FOR CALCULATION 1
    ADDING TWO NUMBERS:
    Enter the two numbers to Add
    Enter A: 2
    Enter B: 5
    7
    ENTER THE CORRESPONDING NUMBER FOR CALCULATION 2
    SUBTRACTING TWO NUMBERS
    Enter the two numbers to Subtract
    Enter A: 2
    Enter B: 5
    -3
    ENTER THE CORRESPONDING NUMBER FOR CALCULATION 3
    MULTIPLYING TWO NUMBERS
    Enter the two numbers to Multiply
    Enter A: 2
    Enter B: 5
    10
    ENTER THE CORRESPONDING NUMBER FOR CALCULATION 4
    DIVIDEING TWO NUMBERS
    Enter the two number to Divide
    Enter A: 2
    Enter B: 5
    0.4
    ENTER THE CORRESPONDING NUMBER FOR CALCULATION 0
    

    功能但不完美,例如对错误输入没有错误处理。

答案 1 :(得分:1)

你错过了前一行的结束括号(神秘语法错误的常见原因),更改:

CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))

CHOICE = int(raw_input("ENTER THE CORRESPONDING NUMBER FOR CALCULATION"))

这不是程序中唯一的语法错误 - 当你不应该使用:结束许多行时,例如:

    add(c):
    sub(c):
    Mul(c):
    Div(c):

你也

  1. :语句没有else(需要)
  2. 大写Print应该是print
  3. 在任何函数之外都有一个return语句
  4. 还有一些错误不是语法错误:

    1. miss True true
    2. CHOICE(一个int)与"1""2"
    3. 等字符串进行比较
    4. 将不存在的变量c传递给不带参数的函数

答案 2 :(得分:1)

您正在将变量c传递给函数add() sub()等,但它们被定义为不带参数。

答案 3 :(得分:1)

在已经提到的语法错误的基础上我认为你真正想要的是每个函数将值返回到主程序循环,然后显示它们:

def add():
    A=int(raw_input("Enter A:"))
    B=int(raw_input("ENter B:"))
    return A + B
...

while true:

    CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION")) 

    if CHOICE == "1": 
        print 'ADDING TWO NUMBERS:'
        answer = add()
        print answer
    ...

或者通过在主循环中输入A和B然后将它们作为参数传递给计算函数来缩短程序:

def add():
    return A + B
...

while true:

    CHOICE = int(raw_input(("ENTER THE CORRESPONDING NUMBER FOR CALCULATION")) 
    A=int(raw_input("Enter A:"))
    B=int(raw_input("ENter B:"))
    if CHOICE == "1": 
        print 'ADDING TWO NUMBERS:'
        answer = add(A, B)
        print answer
    ...