Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 属性错误:';int';对象没有属性';移动';_Python 3.x - Fatal编程技术网

Python 3.x 属性错误:';int';对象没有属性';移动';

Python 3.x 属性错误:';int';对象没有属性';移动';,python-3.x,Python 3.x,我正在为我的班级制作一个石头、布、剪刀的游戏。使用 'Int(输入对象)选择CPU策略,但现在不允许 一旦我们进入回合,CPU将在我扔掉我的后通过一个移动。 请帮忙 GitBash中的错误: 回溯(最近一次呼叫最后一次): 文件“rps.py”,第171行,在 游戏。玩单打() 文件“rps.py”,第123行,在播放中 self.play_round() 文件“rps.py”,第86行,在游戏中 move2=self.p2.move() 如何更正此回溯错误 这是我的密码: def __init

我正在为我的班级制作一个石头、布、剪刀的游戏。使用 'Int(输入对象)选择CPU策略,但现在不允许 一旦我们进入回合,CPU将在我扔掉我的后通过一个移动。 请帮忙

GitBash中的错误: 回溯(最近一次呼叫最后一次): 文件“rps.py”,第171行,在 游戏。玩单打() 文件“rps.py”,第123行,在播放中 self.play_round() 文件“rps.py”,第86行,在游戏中 move2=self.p2.move()

如何更正此回溯错误

这是我的密码:

def __init__(self, p2):
    self.p1 = HumanPlayer()
    self.p2 = p2

def play_round(self):
    move1 = self.p1.move()
    move2 = self.p2.move()
    print(f"Player 1: {move1} <> Player 2: {move2}")
    self.p1.learn(move1, move2)
    self.p2.learn(move2, move1)
    if beats(move1, move2):
        self.p1_score += 1
        print('* Player 1 wins! *')
    else:
        if move1 == move2:
            print('* Tie *')
        else:
            self.p2_score += 1
            print('* Player 2 wins! *')

    print(f"Player:{self.p1.__class__.__name__}, Score:{self.p1_score}")
    print(f"Player:{self.p2.__class__.__name__}, Score:{self.p2_score}")

# This will call a tourney
def play_game(self):
    print("Game Start!")
    for round in range(3):
        print(f"Round {round}:")
        self.play_round()
    if self.p1_score > self.p2_score:
        print('** Congrats! Player 1 WINS! **')
    elif self.p2_score > self.p1_score:
        print('** Sadly Player 2 WINS! **')
    else:
        print('** The match was a tie! **')
    print('The final score is: ' + str(self.p1_score) + ' TO ' +
          str(self.p2_score))
    print("Game over!")

# This will call a singe game.
def play_single(self):
    print("Single Game Start!")
    print(f"Round 1 of 1:")
    self.play_round()
    if self.p1_score > self.p2_score:
        print('** Congrats! Player 1 WINS! **')
    elif self.p1_score < self.p2_score:
        print('** Sadly Player 2 WINS! **')
    else:
        print('** The game was a tie! **')
    print('The final score: ' + str(self.p1_score) + ' TO ' +
          str(self.p2_score))


if __name__ == '__main__':
    p2 = {
        "1": Player(),
        "2": RandomPlayer(),
        "3": CyclePlayer(),
        "4": ReflectPlayer()
        }

# Selecting Opponent
while True:
    try:
        p2 = int(input("Select the strategy "
                       "you want to play against:  "
                       "1 - Rock Player "
                       "2 - Random Player "
                       "3 - Cycle Player "
                       "4 - Reflect Player: "))


  #"Select strategy:
    except ValueError:
        print("Sorry, I didn't understand that.")
        continue

    if p2 > 4:
        print("Sorry, you must select [1-4]: ")
        continue
    else:
        break


# Slecting 1 or 3 games
rounds = input('Enter for [s]ingle game or [f]ull game: ')
Game = Game(p2)
while True:
    if rounds == 's':
        Game.play_single()
        break
    elif rounds == 'f':
        Game.play_game()
        break
    else:
        print('Please select again')
        rounds = input('Enter [s] for a single'
                       'game and [f] for a full game: ')
def\uuuuu初始化(self,p2):
self.p1=HumanPlayer()
self.p2=p2
def播放回合(自我):
move1=self.p1.move()
move2=self.p2.move()
打印(f“播放器1:{move1}播放器2:{move2}”)
自我学习(移动1,移动2)
self.p2.学习(move2,move1)
如果跳动(移动1,移动2):
self.p1_分数+=1
打印(“*玩家1获胜!*”)
其他:
如果move1==move2:
打印(“*Tie*”)
其他:
self.p2_分数+=1
打印(“*玩家2获胜!*”)
打印(f“玩家:{self.p1.{class.{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu name},分数:{self.p1.{Score}”)
打印(f“玩家:{self.p2.{class.{uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu name},分数:{self.p2.{Score}”)
#这将是一场比赛
def游戏(自我):
打印(“游戏开始!”)
对于范围内的圆形(3):
打印(f“Round{Round}:)
self.play_round()
如果self.p1\u分数>self.p2\u分数:
打印(“**恭喜!玩家1获胜!**”)
elif self.p2_分数>self.p1_分数:
打印(“**遗憾的是,玩家2赢了!**”)
其他:
打印(“**比赛是平局!**”)
打印('最终分数为:'+str(self.p1_分数)+'TO'+
str(自我评分)
打印(“游戏结束!”)
#这将称为单打游戏。
def播放单(自):
打印(“单一游戏开始!”)
打印(f“第1轮,共1轮:”)
self.play_round()
如果self.p1\u分数>self.p2\u分数:
打印(“**恭喜!玩家1获胜!**”)
elif self.p1_分数4:
打印(“对不起,您必须选择[1-4]:”)
持续
其他:
打破
#挑选1或3场比赛
轮数=输入('输入[s]单个游戏或[f]整个游戏:')
游戏=游戏(p2)
尽管如此:
如果轮数==“s”:
游戏。玩单打()
打破
elif轮数==“f”:
游戏
打破
其他:
打印('请再次选择')
轮数=输入('输入单个'
'游戏和[f]对于完整游戏:')
这条线有几个问题

  • 将由
    Game(p2)
    创建的对象指定给
    Game
    变量会对
    Game
    类产生阴影。这很不幸,因为您以后将无法方便地创建
    Game
    对象。更好的命名方式将为
    Game=Game(p2)
  • 在那一行,
    p2
    是一个整数,因为您的代码以前运行过
    p2=int(输入(…)
    。执行
    Game(p2)
    用整数实例化
    Game
    对象:
    def __init__(self, p2):
    self.p1 = HumanPlayer()
    self.p2 = p2                # now self.p2 is also an int 
    
  • def播放回合(自我): move1=self.p1.move() move2=self.p2.move()#调用一些int.move() ``` 该错误正确地通知您正在尝试对整数调用
    .move
    。类似于
    1.move
    42.move
    。但整数没有
    。move

    要解决此问题,请使用另一个变量而不是
    p2
    ,例如…
    choice
    。这样,
    p2
    可以保持
    dict
    ,而另一个变量
    choice
    可以存储输入的值

    # Selecting Opponent
    while True:
        try:
            choice = int(input("Select the strategy "
                               "you want to play against:  "
                               "1 - Rock Player "
                               "2 - Random Player "
                               "3 - Cycle Player "
                               "4 - Reflect Player: "))
    
      #"Select strategy:
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
    
        if choice > 4:
            print("Sorry, you must select [1-4]: ")
            continue
        else:
            break
    
    ...
    
    game = Game(p2[choice])  # since p2 is a dictionary with int keys and Player values
    
    这条线有几个问题

  • 将由
    Game(p2)
    创建的对象指定给
    Game
    变量会对
    Game
    类产生阴影。这很不幸,因为您以后将无法方便地创建
    Game
    对象。更好的命名方式将为
    Game=Game(p2)
  • 在那一行,
    p2
    是一个整数,因为您的代码以前运行过
    p2=int(输入(…)
    。执行
    Game(p2)
    用整数实例化
    Game
    对象:
    def __init__(self, p2):
    self.p1 = HumanPlayer()
    self.p2 = p2                # now self.p2 is also an int 
    
  • def播放回合(自我): move1=self.p1.move() move2=self.p2.move()#调用一些int.move() ``` 该错误正确地通知您正在尝试对整数调用
    .move
    。类似于
    1.move
    42.move
    。但整数没有
    。move

    要解决此问题,请使用另一个变量而不是
    p2
    ,例如…
    choice
    。这样,
    p2
    可以保持
    dict
    ,而另一个变量
    choice
    可以存储输入的值

    # Selecting Opponent
    while True:
        try:
            choice = int(input("Select the strategy "
                               "you want to play against:  "
                               "1 - Rock Player "
                               "2 - Random Player "
                               "3 - Cycle Player "
                               "4 - Reflect Player: "))
    
      #"Select strategy:
        except ValueError:
            print("Sorry, I didn't understand that.")
            continue
    
        if choice > 4:
            print("Sorry, you must select [1-4]: ")
            continue
        else:
            break
    
    ...
    
    game = Game(p2[choice])  # since p2 is a dictionary with int keys and Player values
    

    感谢您的回复!现在它给了我一个p2的名称错误?game=game(p2[choice])名称错误:名称“p2”不是defined@Marcus我在这里做一些猜测:1)检查文件名是否为
    main.py
    ,否则
    p2={…}
    字典将不会在if块下运行。或2)移动
    p2={…}
    if块外部。删除
    if\na