Python未绑定方法错误

时间:2014-07-10 17:37:47

标签: python methods

有人可以纠正我一直遇到麻烦的错误!问题是它没有在Goalie和Player类中生成随机整数。这个问题并不能让我真正得到两个"玩家"移动。 错误:

import random
def main(): 
    name = raw_input("Name: ")
    kick = raw_input("\nWelcome " +name+ " to soccer shootout! Pick a corner to fire at! Type: TR, BR, TL, or BL! T = TOP B = BOTTOM L = LEFT R = RIGHT:  ")
    if Player.Shot == Goalie.Block:
        print("\nThe goalie dives and blocks the shot!")
    if Player.Shot != Goalie.Block:
        print("\nThe ball spirals into the net!")
        print(Player.Shot)
        print(Goalie.Block)
class Player():
    def __init__(self):
        self.Shot()
    def Shot(self):
        shot = random.randint(0,3)
        self.Shot = shot
class Goalie():
    def __init__(self):
        self.Block()

    def Block(self):
        block = random.randint(0,3)
        self.Block = block
main()

1 个答案:

答案 0 :(得分:2)

您需要首先实例化该类::

尝试:

import random
def main(): 
    name = raw_input("Name: ")
    kick = raw_input("\nWelcome " +name+ " to soccer shootout! Pick a corner to fire at! Type: TR, BR, TL, or BL! T = TOP B = BOTTOM L = LEFT R = RIGHT:  ")
    player=Player() #error fixed:<-- called the class
    goalie=Goalie() #error fixed:<-- called the class
    if player.shot == goalie.block:#error fixed:<-- used that initiated class
        print("\nThe goalie dives and blocks the shot!")
    if player.shot != goalie.block:
        print("\nThe ball spirals into the net!")
        print(player.shot)
        print(goalie.block)

class Player:
    def __init__(self):
        self.Shot()

    def Shot(self):
        shot = random.randint(0,3)
        self.shot = shot #error fixed:<-- attribute and method are same


class Goalie:
    def __init__(self):
        self.Block()

    def Block(self):
        block = random.randint(0,3)
        self.block = block #error fixed:<-- attribute and method are same

main()