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
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.
c
to your function, c
is never initialized, what is c
?def add():
(even though pass the mysterious c
value). print
or return
the result it just computes.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.