简体   繁体   English

Function 未在 Class 中定义

[英]Function not Defined in Class

I am having trouble with a coding project in which I am trying to use classes in python to make a card game (cheat).我在尝试使用 python 中的类来制作纸牌游戏(作弊)的编码项目时遇到问题。 However, when creating the player class, one of the functions that was previously defined within the class is shown as undefined.但是,在创建播放器 class 时,之前在 class 中定义的函数之一显示为未定义。 I cannot figure out the reason, and any suggestion is appreciated.我无法弄清楚原因,任何建议都值得赞赏。 Below is the definition of the classes下面是类的定义

class card(object):
  def __init__(self,rank,suit):
    self.rank = rank
    self.suit = suit

class player(object):
  def __init__ (self):
    self.number = number
    self.hand = list()
    #Here, hand is a list of the card class that was distributed with a suit and a rank
  def check_card(self,player_rank,player_suit):
    for card in self.hand:
      if card.rank == player_rank and card.suit == player_suit:
        return True
        break
    return False
  def play_card(self):
    suit = input('what is the suit?')
    rank = input('what is the rank?')
    if check_card(self,rank,suit):
      print(True)
    else:
      print(False)

Here is the actual code that will run it这是将运行它的实际代码

player = player()
player.play_card()

The following error was received:收到以下错误:

NameError: name 'check_card' is not defined

I have been troubleshooting and looking at different solutions, including moving the functions outside the class, but it continues to display the same error.我一直在排除故障并查看不同的解决方案,包括将功能移到 class 之外,但它继续显示相同的错误。 Can anyone point out the mistake?谁能指出错误? Thanks!谢谢!

You have the following two issues in your code您的代码中有以下两个问题

  • The way you passed self to the check_card function is wrong.您将 self 传递给check_card function 的方式是错误的。 You must call it in this way你必须以这种方式调用它
self.check_card(rank,suit)
  • The second issue is that the number is not defined.第二个问题是没有定义数字。 Thus I passed it as an argument while initializing the player.因此,我在初始化播放器时将其作为参数传递。 Feel free to make changes for that.随时为此进行更改。

This is the corrected code:这是更正后的代码:

class card(object):
  def __init__(self,rank,suit):
    self.rank = rank
    self.suit = suit

class player(object):
  def __init__ (self, number):
    self.number = number
    self.hand = list()
    #Here, hand is a list of the card class that was distributed with a suit and a rank
  def check_card(self,player_rank,player_suit):
    for card in self.hand:
      if card.rank == player_rank and card.suit == player_suit:
        return True
        break
    return False
  def play_card(self):
    suit = input('what is the suit?')
    rank = input('what is the rank?')
    if self.check_card(rank,suit):
      print(True)
    else:
      print(False)


player = player(3)
player.play_card()

Output: Output:

what is the suit?spade
what is the rank?3
False

Based on this document the function call in python class is self.xxxx(args) (xxxx is denoted function name) therefore the correct version of play_card function is shown as following. Based on this document the function call in python class is self.xxxx(args) (xxxx is denoted function name) therefore the correct version of play_card function is shown as following.

enter code here
def play_card(self):
    suit = input('what is the suit?')
    rank = input('what is the rank?')
    if self.check_card(rank,suit):
        print(True)
    else:
        print(False)

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

相关问题 函数名未在 Python 类中定义 - Function name is not defined in a Python class 函数中定义的Python类的状态 - Status of a Python class defined within a function 关于如何调用 python 中 class 中定义的 function 的问题 - Question on how to call a function defined in a class in python 函数(在类中)中的变量仅可用于创建(并在之前定义)的函数 - Variable in a function (in a class) is only available to that function where it was made (and defined before) 为什么在类中定义的函数在其他代码片段中不可见? - Why function defined in class is not visible in other fragments of code? 异常名称 self is not defined 发生在 function<class, method></class,> - Exception name self is not defined occurred in function <class, Method> 如何在调用在 class 中定义的 function 时不获取 object ID - how to not get object ID when calling a function that was defined inside a class 检测 function 是否被定义为 Python 3.x 中 class 的一部分 - Detect if a function was defined as part of a class in Python 3.x python为什么在一个类中定义的一个函数不能在同一类的另一个函数内部调用? - python why one function defined in a class cant be called inside another function in same class? 我如何在同一个类中使用我的函数在我自己的类中定义的生成器? - How can i use the generator which defined in my own class by my function in the same class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM