【问题标题】:Getting weird outputs from my calculator program从我的计算器程序中得到奇怪的输出
【发布时间】:2016-03-15 17:00:36
【问题描述】:

我有以下代码,它是一个计算器界面:

import pygame
import operator
pygame.init()
screen = pygame.display.set_mode((400, 711))
pygame.display.set_caption("INIX")
Calculator_Screen = pygame.image.load("Calculator.Screen.png")
op = {
    "+": operator.add,
    "-": operator.sub,
    "*": operator.mul,
    "/": operator.truediv,
}
def calculator_module():

    events = list(pygame.event.get())
    if not events:
        return 0
    for event in events:
        if event.type == pygame.QUIT:
            Calculator = False
            return Calculator
        elif event.type == pygame.MOUSEBUTTONUP:
            x, y = pygame.mouse.get_pos()
            if x > 17 and x < 107 and y > 445 and y < 530:     #1
                return "1"
            elif x > 108 and x < 198 and y > 445 and y < 530:     #2
                return "2"
            elif x > 199 and x < 290 and y > 445 and y < 530:     #3
                return "3"
            elif x > 17 and x < 107 and y > 336 and y < 443:     #4
                return "4"
            elif x > 108 and x < 198 and y > 336 and y < 443:     #5
                return "5"
            elif x > 199 and x < 290 and y > 336 and y < 443:     #6
                return "6"
            elif x > 17 and x < 107 and y > 268 and y < 334:     #7
                return "7"
            elif x > 108 and x < 198 and y > 268 and y < 334:     #8
                return "8"
            elif x > 199 and x < 290 and y > 268 and y < 334:     #9
                return "9"
            elif x > 17 and x < 107 and y > 532 and y < 620:     #0
                return "0"
            elif x > 199 and x < 290 and y > 532 and y < 620:     #=
                return "="
            elif x > 292 and x < 380 and y > 532 and y < 620:     #+
                return "+"
            elif x > 292 and x < 380 and y > 445 and y < 530:     #-
                return "-"
            elif x > 292 and x < 380 and y > 268 and y < 334:     #/
                return "/"
            elif x > 292 and x < 380 and y > 336 and y < 443:     #x
                return "*"
            else:
                return 0
    return 0

Calculator = True
while Calculator:
    screen.blit(Calculator_Screen, (0, 0))
    pygame.display.update()
    events = list(pygame.event.get())
    for event in events:
        if event.type == pygame.QUIT:
            Calculator = False
        if event.type == pygame.MOUSEBUTTONUP:
            x, y = pygame.mouse.get_pos()
            if x > 180 and x < 218 and y > 670 and y < 708:
                Calculator = False

            while True:
                current = 0
                num1 = 0
                num2 = 0

                while current not in op:
                    num1 = num1*10 + int(current)
                    current = calculator_module()
                last_op = current
                current = 0
                while current != "=":
                    if current in op:
                        num1 = op[last_op](num1, num2)
                        last_op = current
                        num2 = 0
                    else:
                        num1 = num1*10 + int(current)
                    current = calculator_module()
                res = op[last_op](num1, num2)
                print(res)

我在代码中遇到了一个问题,即当我执行 4-2 之类的简单操作时,我得到了类似 4000000000000000000000000000000000000... 的结果。我试图用下面的一些答案来解决它,但不能。如果您能提供帮助,将不胜感激。

【问题讨论】:

  • 我怀疑这是您的问题的原因,但您可以使用 Python 的运算符链来简化鼠标位置检查代码。 x &gt; 17 and x &lt; 107 相当于更清晰的17 &lt; x &lt; 107

标签: python-3.x pygame


【解决方案1】:

你得到这么多的 0,因为你将每个 while 循环的 num1 与 10 相乘:

num1 = num1*10 + int(current)

即使您的代码还有更多问题,请从以下开始:

在初始化部分做:

current = -1

改变

    num1 = num1*10 + int(current)
    current =

    if current >= 0:
         num1 = num1*10 + int(current)
    current = -1

正如我所说,还有很多事情要做。所以请继续关注;)


您应该对 pygame.MOUSEBUTTONUP 进行操作,而不是对 pygame.MOUSEBUTTONDOWN,因为“down”具有自动重复功能。

您可以通过第一个即将发生的 DOWN 事件来保护目标区域,并忽略进一步的 DOWN 事件,直到 UP 事件到达。你可以检查DOWN-click-area和UP-click-area是否属于同一个按钮,然后才继续。

【讨论】:

  • 据我了解,在找到操作员之前,您不会离开第一个 while current not in op。所以如果你得到很多'0',它们一定是按钮'0'区域中鼠标按下事件的结果。或者您必须更具体地说明您与程序进行的哪些交互会产生不想要的结果。
  • 不,这不是原因,您的评论中有很多错误。我编辑了我的答案以针对您的关键问题
  • 你不能只是替换它,你的程序逻辑需要一个过孔
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多