Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi! I'm new to programming and I need help with my phyton code for a calculator. Here's what the calculator needs to do:
 
- Every time the function runs, it takes three inputs: first number, the operation (either of +, -, * or /). 
If the operation is anything other than the above-given ones, a suitable message shows up 
The result of the operation is displayed at the end.
- *** The calculator should keep running unless interrupted by Ctrl-C. 
- *** The function should never crash. Include exception handling at suitable locations to avoid falling into any errors (exceptions).

 - ***The file you submit should have the function call to this function too.
What I marked with *** is what I'm having the most trouble with.

This is the calculator running:
<pre>
Python


>>> calc()
23
K
90
Unknown Operation

What I have tried:

Here's what I have so far: 
>>>def calc():
    a = int(input())
    b = input()
    c = int(input())
    while True:
        try:
            if b== "+" :
                print(a+c)
                break
            elif b == "-":
                print(a - c)
                break
            elif b == "*":
                print(a*c)
                break
            elif b == "/":
                print(a / c)
                break
            else:
                print("Unknown Operation")
                break
Posted
Updated 31-Jan-23 19:49pm

1 solution

Move the input statements inside the loop ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900