Getting weird outputs from my calculator program

2019-09-01 09:56发布

问题:

I have the following code which is a calculator interface:

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)

I have encountered a problem in my code which is that I get results like 4000000000000000000000000000000000000... when I do a simple operation like 4-2. I have tried to fix it with some of the answers below but could not. If you can help that would be greatly appreciated.

回答1:

You get such much 0's because you multiply num1 each while loop with 10:

num1 = num1*10 + int(current)

Even if there are some more problems with your code, start with this:

In the init-section do:

current = -1

Change

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

to

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

As I say, there is much more to do. So stay tuned ;)


You should act on pygame.MOUSEBUTTONUP, not on pygame.MOUSEBUTTONDOWN, because "down" has an autorepeat function.

You may safe the target area by the first upcoming DOWN event and ignore further DOWN events until an UP event arrives. You than may check if the DOWN-click-area and the UP-click-area belongs to the same button and only proceed than.