AttributeError:“ int”对象没有属性“ move”

时间:2019-06-14 03:16:32

标签: python-3.x

我正在为我的课堂创建一个石头,纸,剪刀的游戏。使用了          'Int(用于选择CPU策略的输入对象,但现在不允许          一旦我们进入回合,CPU在我扔了我的球之后就通过了一个动作。          请帮忙。

GitBash中的错误:          追溯(最近一次通话):          文件“ rps.py”,第171行,位于          Game.play_single()          play_single中的文件“ rps.py”,第123行          self.play_round()          在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: ')

1 个答案:

答案 0 :(得分:0)

Game = Game(p2)

此行有两个问题。

  1. Game(p2)创建的对象分配给Game类的Game变量阴影。不幸的是,稍后您将无法方便地创建Game对象。更好的命名将使game = Game(p2)
  2. 在那一行,p2是一个整数,因为您的代码以前运行p2 = int(input(...))。进行Game(p2)实例化您的Game对象是一个整数:
    def __init__(self, p2):
    self.p1 = HumanPlayer()
    self.p2 = p2                # now self.p2 is also an int 
    

def play_round(self):     move1 = self.p1.move()     move2 = self.p2.move()#调用some_int.move()     ```     该错误正确地通知您您正在尝试对整数调用.move。与1.move42.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