TransWikia.com

How to make new person in class player?

Stack Overflow Asked by JoshMann15 on November 15, 2021

i have a programm here that i made and i trying to add a new player. Every time i try it comes up with an error could someone please help me.
Thanks in advance,
-Josh

          def main():
            class Player:
                def __init__(self, name, tank_name):
                    self.name = name
                    self.tank_name = tank_name
            def new_player():
                p1 = Player()
                p1.name = input('What is your name >>> ')
                p1.tank_name = input('What is your tanks name >>> ')
            new_player()
        
        if __name__ == '__main__':
            main()

2 Answers

You can also set name and tank_name parameters to None for __init__ method

    def main(): 
        class Player:
                    def __init__(self, name=None, tank_name=None):
                        self.name = name
                        self.tank_name = tank_name
                def new_player():
                    p1 = Player()
                    p1.name = input('What is your name >>> ')
                    p1.tank_name = input('What is your tanks name >>> ')
                new_player()
            
   if __name__ == '__main__':
       main()

Answered by klv0000 on November 15, 2021

name and tank_name are required parameters for the __init__ method. You need to ask for them before creating the Player, not assign them after.

class Player:

    def __init__(self, name, tank_name):
        self.name = name
        self.tank_name = tank_name

def new_player():
    name = input('What is your name >>> ')
    tank_name = input('What is your tanks name >>> ')
    p1 = Player(name, tank_name)

new_player()

Answered by Barmar on November 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP