2015-10-17 120 views
-1

我想制作一个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() 
+3

没有错误?这应该至少打印“你好用户”。 – Tim

+0

你可以把if(operator!=“ - ”和...)写成'如果操作不在“ - + * /”'中,但是因为你已经测试了它们,为什么不操作'if operation = =' - ':...; elif操作=='+':...; ...;否则:#处理错误的输入' –

+1

您需要检查'global'是如何工作的。参见[http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python]。 – MaxPowers

回答

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也被无用地用于add()if(operation == "/") if语句。最后,它不打印任何东西的原因是因为实际上没有打电话给print()。例如,做print(add(num1, num2))而不是只有add(num1, num2)

+0

您也可以删除这些括号。 'if(condition)'在python中不是惯用的。相反,写'如果条件' –

+0

是的,只是做他自己,加上我太习惯java了。 – RobertR

0

的问题是,你只需要调用您的计算功能(加,乘,...),他们返回一个值。但为了看到这个价值,你也需要打印它!所以,你的代码看起来像:

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

...