Python Forum
Failing to Build a Calculator
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Failing to Build a Calculator
#1
hi guys.
just started to work with python, first time programming.
anyway, I got a task to build a simple calculator, that can use the operators (*,-, + , / , ^ )
attached the code here:
    while True:
    print("welcome to Yotaam's math")
    print("enter 'add' to add two numbers")
    print("enter 'mtpy' to multiply two numbers")
    print("enter 'sub' to suctract two numbers")
    print("enter 'dvd' to divide two number")
    print("enter 'pwr' to put a number in the power of a second number")
    print("enter 'quit' to end the program")
    input(":") 
    if  input() == "quit":
        break
    elif input == "add":
        num1 = float(input("type first number :")) 
        num2 = float(input("type second number:"))
        rslt = num1 + num2
        print ("The result is:" + rslt)
    elif input == "mtpy":
        num1 = float(input("type first number")) 
        num2 = float(input("type second number:"))
        rslt = num1 * num2
        print ("The result is:" + rslt)
    elif input == "sub":
        num1 = float(input("type first number:")) 
        num2 = float(input("type second number:"))
        rslt = float(num1 - num2)
        print ("The result is:" + rslt)
    elif input == "dvd":
        num1 = float(input("type first number:")) 
        num2 = float(input("type second number:"))
        rslt = num1 / num2
        print ("The result is:" + rslt)
    elif input == "pwr":
        num1 = float(input("type first number:")) 
        num2 = float(input("type second number:"))
        rslt = num1 ^ num2
        print ("The result is:" + rslt)
    else:
        print ("unknown input, try again") 
For some reason, when i run the program, i get the : instruction. when i type in when of the options i made i get an empty screen instead of an input option.
a
ny suggestions? and i use Spyder 4 (also tried it in an online reader)
Reply
#2
You are using input wrong:

equation=input(":")
if equation == "quit":

If you want to make a calculator why not write one where you can enter equations like this:
5*2
3^8
7+6
5-9

How would you go about parsing those commands to get the two numbers and the operators?
Reply
#3
Thanks for the quick answer,i tried it:
 if  "quit" == input(":"): 
        break
        elif "add" == input:
        num1 = float(input("type first number :")) 
        num2 = float(input("type second number:"))
        rslt = num1 + num2
        print ("The result is:" + rslt)
unfortunately still not working

About your suggestion, It was my first program that i wrote, so i thought about a simiiliar idea only after i wrote it, but its a good idea.
Reply
#4
You seem to have some confusion about "input". The input function prints a prompt, and returns what the user types. It is a function. If you want "input" to get input from the user you need to include the "()". The parenthesis tell the program to execute the function. When your program encounters "elif 'add' == input:" the program reads this as "Is 'add' equal to the input function?" A str is never going to be equal to a function. It may equal the value returned when a function is executed, but it will never equal a function.

You are using "input" correctly to get the numbers, saving the return value in a variable. You need to save the operator too.
...
print("enter 'pwr' to put a number in the power of a second number")
print("enter 'quit' to end the program")
while True:
    op = input("Operator : ") 
    if  op == "quit":
        break  # This does not make the program quit.
    else:
        num1 = float(input("first operand:  "))  # Avoid duplicate code
        num2 = float(input("second operand: "))  # All operations need 2 operands
        if op == "add":
            print (f'{num1} + {num2} = {num1+num2}')
...
And you will have to work on your quit logic. As is typing "quit" does not end the program.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  conditional assignment failing charlottecrosland 5 3,879 Aug-07-2017, 04:31 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020