Python输出空白

时间:2015-10-17 18:44:26

标签: python

我正在尝试制作一个python计算器。 它一直有效,直到它试图计算。 计算得出一个空白输出。 我尝试将一些变量设为全局,但这不起作用。我的想法已经不多了。如果你知道我的错误,请回答。 这是我的代码:

#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()

2 个答案:

答案 0 :(得分:0)

问题是你只需调用你的计算函数(add,multiply,...),它们就会返回一个值。但是为了看到这个价值你也需要打印它!所以,你的代码看起来像:

...
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

...

答案 1 :(得分:0)

你的代码有很多问题,但是因为它看起来像你正在学习,我不会只是给你答案,但我会给你一些指示。首先,这条线是一团糟:

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

用以下方式将其切换出来:

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

此外,您的主要功能(calculator())应该在循环中,或者至少是输入部分(operation = input("Wat wil je doen: - aftreken, + optellen, * vermeningvuldigen, / delen "))。现在,如果用户没有输入有效的操作,程序就会结束,因为它会输入if语句,这会导致函数结束,最终导致程序结束。而且,全局变量的使用非常混乱。您应该将text1text2作为参数传递给userinput()。在text1text2 if语句中也无用地使用add()if(operation == "/")。最后,它没有打印任何东西的原因是因为实际上没有调用print()。例如,执行print(add(num1, num2))而不只是add(num1, num2)