代码之家  ›  专栏  ›  技术社区  ›  Gusta

如何调用函数:def(dot)和def(dice)from def(main)?

  •  1
  • Gusta  · 技术社区  · 5 年前

    从第三个(主)函数调用前两个函数时遇到问题。我相信我的代码是正确的(忽略大小和x,y坐标)来显示di的五个边,但似乎无法找出如何调用函数。

    主要目的是显示DI的五个面。

    def dot(c,d):
     #draw each dot 
     circDot1 = Circle(Point(50, 50), 5).setFill('black')
     circDot1.draw(win)
     circDot3 = Circle(Point(50, 100), 5).setFill('black')
     circDot3.draw(win)
     circDot5 = Circle(Point(100, 50), 5).setFill('black')
     circDot5.draw(win)
     circDot7 = Circle(Point(100, 100), 5).setFill('black')
     circDot7.draw(win)
     circDot4 = Circle(Point(75, 75), 5).setFill('black')
     circDot4.draw(win)
    
    def dice(a,b):
    
     #build the dice
     rectDice = Rectangle(Point(115, 115), Point(275,275))
     rectDice.setFill('pink')
     rectDice.setOutline('pink')
     rectDice.draw(win)
    
    
    def main():
     # Create window, call dice function
     win = GraphWin("Dice", 400, 400)
     win.setBackground("cyan")
     dice("a","b")
    
    2 回复  |  直到 5 年前
        1
  •  1
  •   furas    5 年前

    几乎没有问题:

    你不执行 main() 运行程序,但也许你没有添加这个只是个问题。


    win 是局部变量,函数不能访问它们。您应该在错误消息中看到它为 NameError: name 'win' is not defined 在里面 rectDice.draw(win) .

    Traceback (most recent call last):
      File "<pyshell#2>", line 30, in <module>
        main()
      File "<pyshell#2>", line 28, in main
        dice("a","b")
      File "<pyshell#2>", line 21, in dice
        rectDice.draw(win)
    NameError: name 'win' is not defined
    

    您可以添加 作为论据

    def dice(win, a, b):
        #build the dice
        rectDice = Rectangle(Point(115, 115), Point(275,275))
        rectDice.setFill('pink')
        rectDice.setOutline('pink')
        rectDice.draw(win)
    
    def main():
        # Create window, call dice function
        win = GraphWin("Dice", 400, 400)
        win.setBackground("cyan")
        dice(win, "a","b")
    

    或者必须将窗口分配给全局变量-使用 global 在里面 主体()

    def dice(a, b):
        #build the dice
        rectDice = Rectangle(Point(115, 115), Point(275,275))
        rectDice.setFill('pink')
        rectDice.setOutline('pink')
        rectDice.draw(win)
    
    def main():
        global win
    
        # Create window, call dice function
        win = GraphWin("Dice", 400, 400)
        win.setBackground("cyan")
        dice("a", "b")
    

    同样的问题 dot()


    点() 你还有其他问题。你必须使用 setFill() 在下一行

    circDot1 = Circle(Point(50, 50), 5)
    circDot1.setFill('black')
    circDot1.draw(win)
    

    完整代码:

    from graphics import *
    
    def dot(win, c, d):
        #draw each dot 
        circDot1 = Circle(Point(50, 50), 5)
        circDot1.setFill('black')
        circDot1.draw(win)
    
        circDot3 = Circle(Point(50, 100), 5)
        circDot3.setFill('black')
        circDot3.draw(win)
    
        circDot5 = Circle(Point(100, 50), 5)
        circDot5.setFill('black')
        circDot5.draw(win)
    
        circDot7 = Circle(Point(100, 100), 5)
        circDot7.setFill('black')
        circDot7.draw(win)
    
        circDot4 = Circle(Point(75, 75), 5)
        circDot4.setFill('black')
        circDot4.draw(win)
    
    def dice(win, a, b):
        #build the dice
        rectDice = Rectangle(Point(115, 115), Point(275,275))
        rectDice.setFill('pink')
        rectDice.setOutline('pink')
        rectDice.draw(win)
    
    
    def main():
        # Create window, call dice function
        win = GraphWin("Dice", 400, 400)
        win.setBackground("cyan")
        dice(win, "a", "b")
        dot(win, "c", "d")
    
    main() 
    
        2
  •  -1
  •   lieahau    5 年前

    我认为main()函数甚至都没有被调用。

    试着使用这个:

    def main():
     # Create window, call dice function
     win = GraphWin("Dice", 400, 400)
     win.setBackground("cyan")
     dice("a","b")
    
    if __name__== "__main__":
     main()
    

    参考: this link