从我的计算器程序获得奇怪的输出

时间:2016-03-15 17:00:37

标签: python-3.x pygame

我有以下代码,它是一个计算器接口:

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...之类的结果。我试图用下面的一些答案解决它,但不能。如果你能提供帮助,我们将不胜感激。

1 个答案:

答案 0 :(得分:2)

你得到这么多0,因为你在每次循环时将num1乘以10:

num1 = num1*10 + int(current)

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

在init-section中执行:

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是否属于同一个按钮并且只进行。