InsideDarkWeb.com

How to make new person in class player?

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()

Stack Overflow Asked by JoshMann15 on November 15, 2021

2 Answers

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!

Related Questions

javascript get Pacific Timezone

1  Asked on December 7, 2020

   

Why define a struct with single private field of unit type?

1  Asked on December 6, 2020 by m-bat

 

Read more Laravel

1  Asked on December 6, 2020 by user11916445

   

npx: the shell-auto-fallback argument has been removed

2  Asked on December 6, 2020 by mike-earley

         

Cross-Process Memory Filesystem In Python

0  Asked on December 6, 2020 by eliran-abdoo

         

HTML Template not rendering on screen

1  Asked on December 6, 2020 by ojm

     

WOE in a magrittr Pipe

2  Asked on December 6, 2020 by gojomoso

 

Convert object of arrays to array of objects

1  Asked on December 5, 2020 by drmrbrewer

       

Remove item from state

3  Asked on December 5, 2020 by clayton-crockville

 

How to concatenate 3 lists even if there are a list null?

2  Asked on December 5, 2020 by alexz

       

Angular 8: Select first element of ng-content

1  Asked on December 5, 2020 by ijustdev

     

docker-compose volume empty

3  Asked on December 5, 2020 by kerby82

   

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.