从我的计算器程序中获取奇怪的输出 [英] Getting weird outputs from my calculator program

查看:64
本文介绍了从我的计算器程序中获取奇怪的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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)

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

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.

推荐答案

你得到这么多 0 是因为你将 num1 与 10 相乘:

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:

在初始化部分做:

current = -1

改变

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

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

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

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

您应该对 pygame.MOUSEBUTTONUP 采取行动,而不是对 pygame.MOUSEBUTTONDOWN 采取行动,因为down"具有自动重复功能.

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

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

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.

这篇关于从我的计算器程序中获取奇怪的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆