-1

I am trying to make a python calculator. It works until it tries to calculate. The calculation gives a blank output. I tried making some variables global but that did not work. I am running out of ideas. Please answer if you know what my mistake is. This is my code:

#import getpass module to get windows username
import getpass
#import time module for delays
import time

#function that sets value1 and value2
def userinput():
    global text1
    global text2
    num1 = int(input(text1))
    num2 = int(input(text2))
    global num1
    global num2

#function that adds input values
def add(num1, num2):
    global text1
    global text2
    return num1 + num2

#function that subtracts input values   
def subtract(num1, num2):
    return num1 - num2

#function that multiplies input values
def multiply(num1, num2):
    return num1 * num2

#function that devides input values
def devide(num1, num2):
    return num1 / num2
#gets user input and looks for invalid input
def calculator():
    name = getpass.getuser()
    print("Hello", name)
    operation = input("Wat wil je doen: - aftreken,  + optellen, * vermeningvuldigen, / delen ")
    if(operation != '-' and operation != '+' and operation != '*' and operation != '/'):
        print("Het symbool dat u getypt hebt stond was niet genoemd")
        time.sleep(1)
        operation = input("Wat wil je doen: - aftreken,  + optellen, * vermeningvuldigen, / delen ")
    else:
        #if "-" was choosen
        if(operation == '-'):
            text1 = "Bij welk nummer moet er wat worden afgetrokken?"
            text2 = "Wat wordt er van afgetrokken?"
            userinput()
            subtract(num1, num2)
        #if "+" was choosen
        if(operation == '+'):
            text1 = "Bij welk nummer moet er wat bij komen?"
            text2 = "Wat wordt komt er bij?"
            userinput()
            add(num1, num2)

        #if "*" was choosen
        if(operation == '*'):
            text1 = "Bij welk nummer moet vermeningvuldigd worden?"
            text2 = "Hoe vaak?"
            userinput()
            multiply(num1, num2)

        #if "/" was choosen
        if(operation == '/'):
            text1 = "Bij welk nummer moet gedeeld worden?"
            text2 = "Door wat?"
            global text1
            global text2
            userinput()
            devide(num1, num2)

calculator()
Dank Pepe
  • 5
  • 3
  • 3
    No error at all? That should print at least "Hello user". – Tim Oct 17 '15 at 18:46
  • you can write `if (operator!="-" and ...)` as `if operation not in "-+*/"`, but since you're testing them all already, why not do `if operation == '-': ...; elif operation == '+': ...; ...; else: # handle bad input` – Adam Smith Oct 17 '15 at 18:57
  • 1
    You need to check how `global` works. See [http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python]. – MaxPowers Oct 17 '15 at 18:58

2 Answers2

0

The problem is that you just call your calculation functions (add, multiply, ...) and they return a value. But in order to see that value you need to print it, too! So, your code would look like:

...
if(operation == '-'):
    text1 = "Bij welk nummer moet er wat worden afgetrokken?"
    text2 = "Wat wordt er van afgetrokken?"
    userinput()
    print(subtract(num1, num2))    # <---- calculate and print value

...
MaxPowers
  • 5,235
  • 2
  • 44
  • 69
0

There are many things wrong with your code, but because it looks like you're learning, I'm not going to just give you the answer, but I will give you some pointers. First off this line is a mess:

if(operation != '-' and operation != '+' and operation != '*' and operation != '/'):

Switch it out with:

if(operation not in "+-*/"):

Also, you're main function (calculator()) should be in a loop, or at least the input part (operation = input("Wat wil je doen: - aftreken, + optellen, * vermeningvuldigen, / delen ")). Right now, if the user doesn't enter a valid operation, the program ends because it enters the if statement which leads to the end of your function and, ultimately, the end of your program. Also, the use of globals is very messy. You should instead pass text1 and text2 as parameters to userinput(). text1 and text2 are also used uselessly in add() and the if(operation == "/") if statement. Finally, the reason it's not printing anything is because there isn't actually a call to print(). For example, do print(add(num1, num2)) instead of just add(num1, num2).

RobertR
  • 745
  • 9
  • 27