gpt4 book ai didi

Python 未绑定(bind)方法错误

转载 作者:行者123 更新时间:2023-12-01 05:08:13 24 4
gpt4 key购买 nike

有人可以纠正我一直遇到的错误吗!问题是它没有在 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()

最佳答案

您需要先实例化该类::

尝试:

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()

关于Python 未绑定(bind)方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24682955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com