Python Calculator Program
Solution 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 TwhileTrue:
# 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 printprint"The value Enter value from 1-4"
The code now has no syntax
errors but still has many problems.
- You pass
c
to your function,c
is never initialized, what isc
? - Your function doesn't take arguments
def add():
(even though pass the mysteriousc
value). - Your function doesn't
print
orreturn
the result it just computes. - You store
CHOICE
as anint
are do comparisons with strings so theelse
case is always executed and there is no way to exit the loop (infinite looping).
Fixed code:
#!/usr/bin/pythondefadd():
print"Enter the two numbers to Add"
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A + B
defsub():
print"Enter the two numbers to Subtract"
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A - B
defmul():
print"Enter the two numbers to Multiply"
A=int(raw_input("Enter A: "))
B=int(raw_input("Enter B: "))
return A * B
defdiv():
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"whileTrue:
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:ADDITION2:SUBTRACTION3:MULTIPLICATION4:DIVITION0:QUITENTERTHECORRESPONDINGNUMBERFORCALCULATION1ADDING TWO NUMBERS:EnterthetwonumberstoAddEnter A:2Enter B:57ENTERTHECORRESPONDINGNUMBERFORCALCULATION2SUBTRACTINGTWONUMBERSEnterthetwonumberstoSubtractEnter A:2Enter B:5-3ENTERTHECORRESPONDINGNUMBERFORCALCULATION3MULTIPLYINGTWONUMBERSEnterthetwonumberstoMultiplyEnter A:2Enter B:510ENTERTHECORRESPONDINGNUMBERFORCALCULATION4DIVIDEINGTWONUMBERSEnterthetwonumbertoDivideEnter A:2Enter B:50.4ENTERTHECORRESPONDINGNUMBERFORCALCULATION0
Functional but not perfect, for instance no error handling for erroneous input.
Solution 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
- have no
:
for anelse
statement (it's required) - capitalize
Print
when it should beprint
- have a return statement outside of any function
There are also errors that are not syntax errors:
- misspell
True
astrue
- compare
CHOICE
, an int, to a string like"1"
or"2"
- are passing a non-existent variable
c
to a function that takes no arguments
Solution 3:
You are passing a variable c
to your functions add()
sub()
etc. but they are defined to take no arguments.
Solution 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
...
whiletrue:
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
...
whiletrue:
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
...
Post a Comment for "Python Calculator Program"