python calculator program [closed]

最后都变了- 提交于 2019-12-25 02:16:59

问题


I wrote a simple calculator program by using functions, I don't know what exactly wrong with this code, its showing error. I did possible steps to debug this, but I couldn't.

#!/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"

Error:

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

回答1:


I've have tried to cover all of the problems with your code, of which there are numerous.

Starting with syntax errors:

# 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"

The code now has no syntax errors but still has many problems.

  1. You pass c to your function, c is never initialized, what is c?
  2. Your function doesn't take arguments def add(): (even though pass the mysterious c value).
  3. Your function doesn't print or return the result it just computes.
  4. You store CHOICE as an int are do comparisons with strings so the else case is always executed and there is no way to exit the loop (infinite looping).

Fixed code:

#!/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"

The code is now functional.

Output:

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

Functional but not perfect, for instance no error handling for erroneous input.




回答2:


You're missing an end parenthesis on the previous line (a common cause of mysterious syntax errors), change:

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

to

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

This is not the only syntax error in the program- you end many lines with : when you shouldn't, like:

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

You also

  1. have no : for an else statement (it's required)
  2. capitalize Print when it should be print
  3. have a return statement outside of any function

There are also errors that are not syntax errors:

  1. misspell True as true
  2. compare CHOICE, an int, to a string like "1" or "2"
  3. are passing a non-existent variable c to a function that takes no arguments



回答3:


You are passing a variable c to your functions add() sub() etc. but they are defined to take no arguments.




回答4:


on top of the syntax errors already mentioned what I think you actually want is for each function to return values to the main programme loop, which will then display them:

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
    ...

or alternatively make the programme shorter by inputting A and B in the main loop then passing those as parameters to the calculating functions:

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
    ...


来源:https://stackoverflow.com/questions/13664759/python-calculator-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!