【问题标题】:Expected ")" Pylance预期的“)”Pylance
【发布时间】:2021-05-26 18:35:11
【问题描述】:

我正在用python(Vs代码)编写代码,我有这个错误:

Expected ")"  Pylance 

错误发生在:def main()
我试图运行我的主程序并将其打印到我的屏幕上。我用谷歌搜索并找不到任何解决方案。我该如何解决?

这是我的代码:

#The main driver of our code , this will handle user input and updating the graphics 
def main():  

   p.init ()
   screen = p.display.set_mode((WIDTH,HEIGHT))
   clock = p.time.Clock ()
   screen.fill(p.Color("white"))
   gs= ChessEngine.GameState()
   print(gs.board)
   loadImages() #only do this once, before the whille loop
   running=True
   while running :
    for e in p.event.get():
     if e.type == p.QUIT:
      running =False
 
   drawGameState(screen,gs)
   clock.tick(MAX_FPS)
   p.display.flip() 

 '''
Responsible for all the graphics within a currnet game state.
 '''
 def drawGameState(screen,gs):
  drawBoard (screen) #draw squars on the board
 #add in piece highlighting or move suggestions
 
 drawPieces(screen,gs.board) #draw pieces on the top of those squares

'''
draw the squares on the board the top left square is always light

'''
def drawBoard(screen):
 colors = [p.Color("white"), p.Color("gray")]
 for r in range (DIMENSION):
   for c in range (DIMENSION):
    color = colors[((r+c) % 2)]
    p.draw.rect(screen , color , p.Rect(c*SQ_SIZE,r*SQ_SIZE,SQ_SIZE_SQ_SIZE))

'''
draw the pieces on the board using the current Game.State.board
'''
def drawPieces(screen,board):
    for r in range (DIMENSION):
        for c in range (DIMENSION):
            piece=board[r][c]
            if piece != "--": #not empty squares
                screen.blit(IMAGES[piece],p.Rect(c*SQ_SIZE,r*SQ_SIZE))
 

if __name__ == '__main__':
   main()

【问题讨论】:

  • 哪一行导致错误?据我所知,main 中没有遗漏任何 )
  • def main(): 这里
  • 我没有收到任何错误。 main前面有代码吗?
  • from pygame.constants import QUIT from ChessEngine import GameState import pygame as p #from Chess import ChessEngine #import ChessEngine WIDTH=HEIGHT=512 DIMENSION =8 #棋盘尺寸为8x8 SQ_SIZE=HEIGHT/ /DIMENSION MAX_FPS =15 #用于稍后的动画 IMAGES= {} def loadImages():pieces = ['wp','wR','wN',"wB",'wK','wQ','bp', 'bR','bN','bB','bK','bQ'] 分段:0 IMAGES[piece]= p.transform.scale (p.image.load("image/" + piece + ".png") , (SQ_SIZE,SQ_SIZE)
  • 此评论上方的代码是我的完整代码(此代码在我在问题中写的主要代码之前)

标签: python visual-studio-code pylance


【解决方案1】:
p.transform.scale(p.image.load("image/" + piece + ".png"), (SQ_SIZE, SQ_SIZE)

(SQ_SIZE, SQ_SIZE) 后面少了一个右括号

p.transform.scale(p.image.load("image/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))

【讨论】:

  • 在我的代码中我没有正确复制
  • 我调试它,我再次得到这个错误 5,在 run_file runpy.run_path(target_as_str, run_name=compat.force_str("main")) 文件 "C:\ Users\nadav\AppData\Local\Programs\Python\Python39\lib\runpy.py”,第 267 行,在 run_path 代码中,fname = _get_code_from_file(run_name, path_name) 文件“C:\Users\nadav\AppData\Local\Programs \Python\Python39\lib\runpy.py”,第 242 行,在 _get_code_from_file 代码 = compile(f.read(), fname, 'exec') 文件“c:\Users\nadav\Desktop\Chess\ChessMain.py” , line 23 def main(): ^ SyntaxError: invalid syntax
  • 您可以编辑您的问题以超越您的代码吗?并且附上图片会好很多。
  • 好的,我编辑了我的问题并附上了我的问题的图片
  • 你真的错过了 '(SQ_SIZE, SQ_SIZE)' 之后的右括号。它应该是“(SQ_SIZE,SQ_SIZE))”。 'scale(' 需要右括号。
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多