【问题标题】:Variable Syntax Error变量语法错误
【发布时间】:2019-01-26 05:08:01
【问题描述】:

所以。这个:

import pygame
import time
import random
import sys

itworks = True

pygame.init()
screen = pygame.display.set_mode((600, 600))

bootpng = pygame.image.load('bootimage.png')
bootpng = pygame.transform.scale(bootpng, (600, 600))
backgroundmenu = pygame.image.load('Menu_background.png')
backgroundmenu = pygame.transform.scale(backgroundmenu, ((600, 600))
button = pygame.image.load('technical_information_button')
button = pygame.transform.scale(infobut, ((130, 80))
screen.blit(bootpng, (0, 0))
time.sleep(3)

while itworks == True:
    screen.blit(backgroundmenu, (0, 0))
    screen.blit(tekbutton, (470, 520))
    for event in pygame.event.get():
        if ecent.type == pygame.QUIT:
            itworks = False

    #    if event.type == pygame.MOUSEBUTTONDOWN:
    # Set the x, y postions of the mouse click
    #x, y = event.pos
    #if ball.get_rect().collidepoint(x, y):

    pygame.display.flip()

pygame.quit()

是我的代码。 Buuut .. 出于某种原因,python 在变量按钮第 15 行*处说“无效语法”。我真的不知道该怎么办,我尝试了很多东西,所以我希望我能从这里得到帮助:D

【问题讨论】:

    标签: python variables syntax pygame syntax-error


    【解决方案1】:

    你需要删除一个额外的括号。

    替换:button = pygame.transform.scale(infobut, ((130, 80))

    与:button = pygame.transform.scale(infobut, (130, 80))

    在 Python 中应该总是有一个左括号和右括号。

    编辑:

    补充一点,我强烈建议将您的while itworks == True 替换为while itworks is True。更多信息可以在这里看到:Boolean identity == True vs is True

    基本上,由于相等性不比较引用,if 语句在某些情况下可能不会给出预期的结果。这只是“pythonic”的做事方式。

    【讨论】:

    • Uhh .. 好的,我刚刚发现了问题,我有那个括号的东西,也在它上面,现在它可以工作了(不是真的它说它无法读取文件,但这是我可以做到的修复..)和呃..你如何将答案标记为已接受?
    【解决方案2】:

    再详细一点。

    问题是您在button = pygame.transform.scale(infobut, ((130, 80)) 行中有多余的括号。

    您得到Syntax Error 的原因是python 认为您正在输入variable=(variable_declared="in parenthesis"),这是一个语法错误。

    如果您在button = pygame.transform.scale(infobut, ((130, 80)) 之后注释了所有代码,您会得到一个更解释性的错误:

    import pygame
    import time
    import random
    import sys
    
    itworks = True
    
    pygame.init()
    screen = pygame.display.set_mode((600, 600))
    
    bootpng = pygame.image.load('bootimage.png')
    bootpng = pygame.transform.scale(bootpng, (600, 600))
    backgroundmenu = pygame.image.load('Menu_background.png')
    backgroundmenu = pygame.transform.scale(backgroundmenu, ((600, 600))
    ##button = pygame.image.load('technical_information_button')
    ##button = pygame.transform.scale(infobut, ((130, 80))
    ##screen.blit(bootpng, (0, 0))
    ##time.sleep(3)
    ##
    ##while itworks == True:
    ##    screen.blit(backgroundmenu, (0, 0))
    ##    screen.blit(tekbutton, (470, 520))
    ##    for event in pygame.event.get():
    ##        if ecent.type == pygame.QUIT:
    ##            itworks = False
    ##
    ##    #    if event.type == pygame.MOUSEBUTTONDOWN:
    ##    # Set the x, y postions of the mouse click
    ##    #x, y = event.pos
    ##    #if ball.get_rect().collidepoint(x, y):
    ##
    ##    pygame.display.flip()
    ##
    ##pygame.quit()
    

    现在,代码抛出了错误SyntaxError: unexpected EOF while parsing,这是由多余的括号直接引起的。

    也就是说,我实际上建议您将while itworks == True: 更改为while itworks:(与@UltraGold 的回答相反)。

    根据this postPEP8(Python 的样式指南),最“Pythonic”的方法是while itworkswhile itworks == True:,实际上进一步劝阻while itworks is True,因为这实际上是一个可能导致意外错误的不同测试(@UltraGold 试图解决的问题)。

    【讨论】:

      猜你喜欢
      • 2015-08-02
      • 2021-08-20
      • 2018-07-08
      • 2014-09-22
      • 1970-01-01
      • 1970-01-01
      • 2015-09-20
      • 1970-01-01
      • 2012-10-17
      相关资源
      最近更新 更多