Python 如何在计算器应用程序中保留内存中的数字?
我正在制作一个带有GUI的计算器,但我不知道如何跟踪一个数字,以便以后可以将其与第二个数字一起使用来执行操作 我的代码如下:Python 如何在计算器应用程序中保留内存中的数字?,python,pygame,calculator,Python,Pygame,Calculator,我正在制作一个带有GUI的计算器,但我不知道如何跟踪一个数字,以便以后可以将其与第二个数字一起使用来执行操作 我的代码如下: def Sum(): Plus = pygame.image.load(os.path.abspath("plus.png")) screen.blit(Plus,(555,34)) c=a+b return c def Sustraction(): Minus = pygame.image.load(os.path.abspat
def Sum():
Plus = pygame.image.load(os.path.abspath("plus.png"))
screen.blit(Plus,(555,34))
c=a+b
return c
def Sustraction():
Minus = pygame.image.load(os.path.abspath("minus.png"))
screen.blit(Minus,(555,34))
c=a-b
return c
def Multiplication():
Times = pygame.image.load(os.path.abspath("Times.png"))
screen.blit(Times,(555,34))
c=a*b
return c
def Division():
Division = pygame.image.load(os.path.abspath("Division.png"))
screen.blit(Division,(555,34))
c=a/b
return c
def button (msg, x, y, w, h, ic, ac, action=None ):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if (x+w > mouse[0] > x) and (y+h > mouse[1] > y):
pygame.draw.rect(CALCULATOR, WHITE, (x, y, w, h))
if (click[0] == 1 and action != None):
if (action == "Sum"):
Sum()
elif (action == "Substraction"):
Substraction()
elif (action == "Multiplication"):
Multiplication()
elif (action == "Division"):
Division()
if (action == "One"):
One()
a=1
return a
elif (action == "Two"):
Two()
a=2
return a
elif (action == "Three"):
Three()
a=3
return a
正如您在我的函数定义中所看到的,当我调用Sum
时,我使用c=a+b
。问题是我不知道如何将a
保存在内存中,以便将其用于b
的求和操作,因此我不知道如何输入新值并将其保存在b
中
当用户在
b
中输入新值时,如何存储a
的值?首先,您应该尝试使用
如果是关于你的问题。只需使用函数和参数。
使用和函数的示例:
def Sum(a, b):
Plus = pygame.image.load(os.path.abspath("plus.png"))
screen.blit(Plus,(555,34))
c=a+b
return c
若你们想“实时”完成,你们可以只使用一个参数,并将结果作为类成员
def Sum(a):
Plus = pygame.image.load(os.path.abspath("plus.png"))
screen.blit(Plus,(555,34))
self.result += a
如果还不够,请阅读
在Python中,只在函数中引用的变量是
隐式全局。如果为变量指定了一个值,则
函数体,假定它是局部函数,除非显式
宣布为全球。
-
因此,当您在函数中指定变量a
和c
时,会将其创建为局部变量。如果您在分配任务之前尝试使用它,例如:
def increment():
c += 1
return c
class Calculator:
def __init__(self) -> None:
self.x = 0
self.result = 0
self.screen = ... # Your initialization
self.actions = {
"Sum": self.sum,
"Substraction": self.subtract,
"Multiplication": self.multiply,
"Division": self.divide
}
self.numbers = {
"One": 1,
"Two": 2,
"Three": 3
}
def sum(self, x):
plus = pygame.image.load(os.path.abspath("plus.png"))
self.screen.blit(plus, (555, 34))
self.result += x
def subtract(self, x):
minus = pygame.image.load(os.path.abspath("minus.png"))
self.screen.blit(minus, (555, 34))
self.result -= x
def multiply(self, x):
times = pygame.image.load(os.path.abspath("Times.png"))
self.screen.blit(times, (555, 34))
self.result *= x
def divide(self, x):
division = pygame.image.load(os.path.abspath("Times.png"))
self.screen.blit(division, (555, 34))
if x != 0:
self.result /= x
else:
print("I refuse to divide by 0.")
def button(self, msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if ((x + w) > mouse[0] > x) and ((y + h) > mouse[1] > y):
pygame.draw.rect(CALCULATOR, WHITE, (x, y, w, h))
if (click[0] == 1) and (action is not None):
if action in self.actions:
self.actions[action]()
elif action in self.numbers:
self.x = self.numbers[action]
您将得到UnboundLocalError:赋值前引用的局部变量“c”
exception
我认为在您的示例中,如果您在执行button()
回调之前调用Sum()
,您也会得到一个NameError:name“a”未定义
异常,因为a
尚未定义。这是因为a
在函数中被引用,但未赋值-因此不会创建为局部变量,但在全局命名空间中也找不到它
最简单的解决方案是使用全局变量。并不是说,每当您想使用全局变量而不是创建局部变量时,都需要在局部上下文中使用global
关键字来显示它:
a = 0
b = 0
c = 0
def sum():
global a, b
Plus = pygame.image.load(os.path.abspath("plus.png"))
screen.blit(Plus,(555,34))
c = a + b
return c
def button(msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
global a
if ....
但是,我建议使用类和存储值作为属性,例如:
def increment():
c += 1
return c
class Calculator:
def __init__(self) -> None:
self.x = 0
self.result = 0
self.screen = ... # Your initialization
self.actions = {
"Sum": self.sum,
"Substraction": self.subtract,
"Multiplication": self.multiply,
"Division": self.divide
}
self.numbers = {
"One": 1,
"Two": 2,
"Three": 3
}
def sum(self, x):
plus = pygame.image.load(os.path.abspath("plus.png"))
self.screen.blit(plus, (555, 34))
self.result += x
def subtract(self, x):
minus = pygame.image.load(os.path.abspath("minus.png"))
self.screen.blit(minus, (555, 34))
self.result -= x
def multiply(self, x):
times = pygame.image.load(os.path.abspath("Times.png"))
self.screen.blit(times, (555, 34))
self.result *= x
def divide(self, x):
division = pygame.image.load(os.path.abspath("Times.png"))
self.screen.blit(division, (555, 34))
if x != 0:
self.result /= x
else:
print("I refuse to divide by 0.")
def button(self, msg, x, y, w, h, ic, ac, action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if ((x + w) > mouse[0] > x) and ((y + h) > mouse[1] > y):
pygame.draw.rect(CALCULATOR, WHITE, (x, y, w, h))
if (click[0] == 1) and (action is not None):
if action in self.actions:
self.actions[action]()
elif action in self.numbers:
self.x = self.numbers[action]
在本例中,我还使用了关键字和值字典,这简化了选择,而不是使用一堆
if
-elif
案例。首先,所有操作函数都应该参数化,例如def Sum(a,b):;c=a+b;返回a+b
。这样,您就可以使用a、b变量的多个值。同样,在代码中,您应该首先定义要执行的操作,然后设置变量a、b等,然后调用Sum(a、b)
或减法(a、b)
或乘法(a、b)
或基于动作的任何其他操作。我不知道后缀符号是如何关联的。我的意思是反转中缀表达式并将其存储为后缀,以便在按下“=”按钮之前不进行任何操作。