简体   繁体   English

当我尝试运行这段代码时,我不断收到命名错误(Python)

[英]I keep getting a naming error when i try to run this piece of code (Python)

class game_type(object):
    def __init__(self):
        select_game = raw_input("Do you want to start the game? ")
        if select_game.lower() == "yes":
            player1_title = raw_input("What is Player 1's title? ").lower().title()


class dice_roll(object,game_type):
    current_turn = 1
    current_player = [player1_title,player2_title]
    def __init__(self):
        while game_won == False and p1_playing == True and p2_playing == True: 
            if raw_input("Type 'Roll' to start your turn  %s" %current_player[current_turn]).lower() == "roll":

I keep getting an error which reads: NameError: name 'player1_title' is not defined 我不断收到错误消息:NameError:未定义名称“ player1_title”

I understand that title is a function so i did try using player1_name and player1_unam but these also returned the same error :( 我知道标题是一个函数,所以我确实尝试使用player1_name和player1_unam,但它们也返回了相同的错误:(

can somebody please help 有人可以帮忙吗

All answers are greatly appreciated 非常感谢所有答案

There are a number of things leading to the NameError. 导致NameError的原因有很多。

For one, the __init__ method of game_type does not save any data. 首先,game_type的__init__方法不会保存任何数据。 To assign instance variables you have to specify the class instance with self. 要分配实例变量,您必须使用self.指定类实例self. . If you don't, you're just assigning local variables. 如果不这样做,那么您只是在分配局部变量。

Secondly, you must explicitly call a parent class's __init__ function with super() if you are creating a new one in a child class and still want the effects of the parent's. 其次,如果您要在子类中创建新的__init__函数,但仍希望获得父类的效果,则必须使用super()显式调用父类的__init__函数。

So basically, your code should be 所以基本上,您的代码应该是

# Class names should be CapCamelCase
class Game(object):                                                                
    def __init__(self):                                                    
        select_game = raw_input("Do you want to start the game? ")         
        if select_game.lower() == "yes":                               
            self.player1_title = raw_input("What is Player 1's title? ").lower().title()
            # Maybe you wanted this in DiceRoll?
            self.player2_title = raw_input("What is Player 1's title? ").lower().title()

# If Game were a subclass of something, there would be no need to 
# Declare DiceRoll a subclass of it as well
class DiceRoll(Game):                                                      
    def __init__(self):                                                   
        super(DiceRoll, self).__init__(self)                               
        game_won = False                                                   
        p1_playing = p2_playing = True                                     
        current_turn = 1                                                   
        current_players = [self.player1_title, self.player2_title]    
        while game_won == False and p1_playing == True and p2_playing == True:
            if raw_input("Type 'Roll' to start your turn %s" % current_players[current_turn]).lower() == "roll":
                pass

暂无
暂无

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

相关问题 当我尝试在终端上运行代码时,我不断收到“ModuleNotFound”错误,即使我安装了它 - I keep getting "ModuleNotFound" error when i try to run code on my terminal even though i installed it 为什么我在尝试运行代码时不断收到导入错误消息? - Why do I keep getting a error message for import when I try to run my code? 当我尝试使用 droidcam 运行我的 CV2 python 代码时出现错误 - When I try to run my CV2 python code with droidcam I am getting error 当我尝试运行我的代码时,我不断收到 TypeError: 'str' object 不能解释为 integer - I keep getting TypeError: 'str' object cannot be interpreted as an integer when i try to run my code 超出时间限制错误 - 当我尝试运行 Python 3 代码时 - Time limit exceeded Error - When I try to run Python 3 code 为什么我在这段python代码中收到意向错误? - Why I am getting intendation error for this piece of python code? 为什么我在 python3 中的这段代码会出现这个错误? - Why am i getting this error with this piece of code in python3? 当我尝试解压文件时,我在 Python 2.7.9 中不断收到 EOF 错误 - I keep getting an EOF error in Python 2.7.9 when I try to unpickle a file 当我尝试导入乌龟时,我不断收到错误消息 - i keep getting an error when i try and import turtle 当我尝试使用 python 和 django 重定向到购物车时,我不断收到此错误 - I keep getting this error when i try to redirect to cart using python and django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM