我想制作一个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()
没有错误?这应该至少打印“你好用户”。 – Tim
你可以把if(operator!=“ - ”和...)写成'如果操作不在“ - + * /”'中,但是因为你已经测试了它们,为什么不操作'if operation = =' - ':...; elif操作=='+':...; ...;否则:#处理错误的输入' –
您需要检查'global'是如何工作的。参见[http://stackoverflow.com/questions/4693120/use-of-global-keyword-in-python]。 – MaxPowers