简体   繁体   English

Python无法调用函数

[英]Python cant call a function

I am developing a rock paper scissors on python and I am stuck. 我在python上开发一个石头剪刀,我被卡住了。 I made a class that cycles between (rock, paper, and scissors ), I want the computer to know it's previous play. 我做了一个在(摇滚,纸和剪刀)之间循环的课程,我希望计算机知道它以前的播放。 for instance ( first round computer played rock, in the next round it should play paper) 比如(第一轮电脑玩摇滚,在下一轮它应该打纸)
but I don't know how to call the learn function to make it work 但我不知道如何调用学习功能使其工作

class Player:
def __init__(self):
    self.score = 0

def move(self):
    return 'rock'

def learn(self, my_move, their_move):
    self.my_move = my_move
    self.their_move = their_move

def beats(one, two):
return ((one == 'rock' and two == 'scissors') or
        (one == 'scissors' and two == 'paper') or
        (one == 'paper' and two == 'rock'))

class Game:
def __init__(self, p1, p2):
    self.p1 = p1
    self.p2 = p2

def play_round(self):
    move1 = input("Pick something!\n")
    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 ("You win")
        print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score) ) 
    elif beats(move2,move1):
        self.p2.score += 1
        print ("Computer wins")
        print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score) ) 
    else:
        print ("Draw")
        print ("Human score = " + str(self.p1.score) + " "  + "Computer score = " + str(self.p2.score) )

def play_game(self):
    print("Game start!")
    for round in range(3):
        print(f"Round {round}:")
        self.play_round()
    print("Game over!")

class human_player(Player):
def move(self):
   return input("Pick something!\n")

class randomplayer(Player):
def move(self):
    return random.choice(moves)

class repeat(Player):
def move(self):
    return 'rock'

class cycleplayer(Player):
def move(self):
    # calling my_move from the Player class
    if self.learn.my_move == "rock" or "paper" or "scissors" :
        return 'rock'

    elif  self.their_move == 'rock':
        return "paper"   

    elif self.their_move == 'paper':
        return "scissors"  

    elif self.their_move == 'scissors':
        return "rock"

if HumanPlayer_choice == "cycle" :
game = Game(Player(), cycleplayer())
game.play_game()

This is the error I am getting. 这是我得到的错误。

Exception has occurred: AttributeError 'function' object has no attribute 'my_move' 发生异常:AttributeError'function'对象没有属性'my_move'

I know that I need to utilize the init function with the learn function to make it work but I not sure how. 我知道我需要利用带有学习功能的init函数来使其工作,但我不确定如何。

The problem is on this line: 问题出在这一行:

if self.learn.my_move == "rock" or "paper" or "scissors" :

learn function does not have an attribute named my_move . learn函数没有名为my_move的属性。 What you meant to do is probably 你打算做什么可能

if self.my_move == "rock" or self.my_move == "paper" or self.my_move == "scissors" :

Note that you have to add self.my_move == before "paper" and "scissors" ; 请注意,您必须在"paper""scissors"之前添加self.my_move == ; otherwise it is evaluated like: 否则评估如下:

if (self.my_move == "rock") or ("paper") or ("scissors"):

and since non-empty strings are always evaluated as True , this if case is rendered useless. 而且由于非空字符串作为评估总是True ,这if情况变得毫无用处。


As @DmitryErohin mentioned, there is a better way of achieving that without repeating yourself: 正如@DmitryErohin所提到的,有一种更好的方法可以实现这一点,而无需重复自己:

if (self.my_move in ("rock", "paper", "scissors")):

This is much less verbose and a lot more readable 这更简洁,更具可读性

There were a few problems in your code as far as I could follow. 据我所知,您的代码中存在一些问题。 Take a look at the example below based on your code. 根据您的代码查看下面的示例。 There's some logic I could not follow so it may not behave exactly as you want. 我无法遵循一些逻辑,因此它可能无法完全按照您的意愿行事。 Suggestions: 建议:

  • as you just want to store the previous move for the cycle_player move, store it in the cycle object (instance) not on the Player 因为你只想存储cycle_player移动的上一个移动,将它存储在循环对象(实例)中而不是播放器上
  • as the move method varies make it's implementation specific to a child class (eg human_player, random_player, cycle_player) 因为移动方法的不同使得它的实现特定于子类(例如human_player,random_player,cycle_player)
  • some methods are better as static rather than recreated in each instance methods 有些方法比静态更好,而不是在每个实例方法中重新创建

     import random class Player: def __init__(self): self.score = 0 class Game: def __init__(self, p1, p2): self.p1 = p1 self.p2 = p2 @staticmethod def check_result(a, b): #Return win, draw, loose for a over b if (a == b): return 'draw' if (a == 'rock' and b == 'scissors') or (a == 'scissors' and b == 'paper') or (a == 'paper' and b == 'rock'): return 'win' return 'loose' def play_round(self): #move1 = input("Pick something!\\n") move1 = self.p1.move() move2 = self.p2.move() print(f"Player 1: {move1} Player 2: {move2}") result = Game.check_result(move1, move2) # a over b if result == 'win': self.p1.score += 1 print ("You win") elif result == 'loose': self.p2.score += 1 print ("Computer wins") else: print ("Draw") print ("Human score = " + str(self.p1.score) + " " + "Computer score = " + str(self.p2.score)) def play_game(self): print("Game start!") for round in range(3): print(f"Round {round + 1}:") self.play_round() print("Game over!") class human_player(Player): def move(self): return input("Pick something!\\n") #TODO validate input class random_player(Player): #Option in game for computer to make random moves def move(self): return random.choice(["scissors","paper","rock"]) class repeat_player(Player): #Option def move(self): return 'rock' class cycle_player(Player): def __init__(self): self.previous_move = None super().__init__() def move(self): # calling my_move from the Player class if self.previous_move is None : next_move = 'rock' elif self.previous_move == 'rock': next_move = "paper" elif self.previous_move == 'paper': next_move = "scissors" elif self.previous_move == 'scissors': next_move = "rock" self.previous_move = next_move return next_move #game = Game(human_player(), random_player()) #game = Game(human_player(), repeat_player()) game = Game(human_player(), cycle_player()) game.play_game() 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM