【问题标题】:Getting a TypeError for unknown reason因未知原因获取 TypeError
【发布时间】:2016-07-01 11:48:21
【问题描述】:

我有以下代码(它是一个计算器程序):

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())
    for event in events:
        if event.type == pygame.QUIT:
            Calculator = False
            return Calculator
        if 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 "*"

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:
                        num2 = num2*10 + int(current)
                    current = calculator_module()
                res = op[last_op](num1, num2)
                print(res)

每当我运行此代码并按下按钮时,我都会收到以下错误: num1 = num1*10 + int(current) TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' 我不知道为什么会出现此错误,因为我根本不使用 None
谢谢。

【问题讨论】:

    标签: python string python-3.x error-handling nonetype


    【解决方案1】:

    请注意,在某些情况下,您的函数 calculator_module() 没有 return(例如,当 events 为空时),因此隐式返回 None。 在这种情况下,您会得到None 作为您的current,这会导致错误。

    要解决此问题,请确保您的函数 calculator_module() 在所有执行路径中返回一个值。

    编辑: 例如:

    def calculator_module():
        events = list(pygame.event.get())
        if not events: # check if the list is empty
            return 0
        for event in events:
            if event.type == pygame.QUIT:
                Calculator = False
                return Calculator
            elif event.type == pygame.MOUSEBUTTONUP:
            ...
            ... # all your other elif go here
            ...
            else:
                return 0
    

    编辑#2:我应该注意到你正在做的看起来很奇怪:

    for event in events:
    

    因为实际上您永远不会迭代超过一个单个事件。既然你会return以防万一......所以你应该重新考虑你的逻辑流程。

    【讨论】:

    • 它是我的calculator_module 在所有if 语句之前的一部分吗?
    • 如我所说,如果events 为空,则可以。或者,如果 event.type != pygame.QUITevent.type != pygame.MOUSEBUTTONUP 对于所有 events
    • 你能告诉我该怎么做吗,因为我理解错误,但仍然对该怎么做有点困惑。
    • 我会编辑我的答案给你看。但这并不意味着我展示的内容将使代码按预期工作,但它会修复None 错误。
    • 为了 1000% 确定,只需在函数末尾添加 return 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
    • 2020-06-25
    相关资源
    最近更新 更多