0

I am making a simple game with multiple players, which each player can insert their first name, last name and each player is assigned 100 poins at the begging. In my code once I am done with coding the "essential" information, but when it comes to user input it does not work.

The "base" for the player class: (this part works)

class Players(): 
    def __init__ (self, firstname, lastname, coins):   #initialising attributes
        self.firstname = firstname
        
        self.lastname = lastname
        
        self.coins= coins
   
    def full_info(self):
      return self.firstname  + self.lastname + self.coins

This is the second part where the problem is, the input is not stored in the attributes

  def get_user_input(self):
        firstname= input("Please enter your first name:")
        lastname= input ("Please enter your second name: ")
        coins= 100 #they are assigned automatically 
        return self(firstname, lastname, coins)

I would appriciate any suggesting regarding the user input.

2
  • Create your minimal reproducible example and add to your question, Commented Dec 2, 2022 at 22:54
  • You never store the info into the attributes... Perhaps you could place your input lines into the init method and store them into the attributes at this time Commented Dec 2, 2022 at 22:57

2 Answers 2

2

Your function to build a new instance from user input should be a classmethod or a staticmethod, since you want to call it to create a new instance.

I'd also suggest using @dataclass so you don't need to copy and paste all the variable names in __init__, and using an f-string in your full_info function so you don't hit an error when you try to add coins to the name strings.

All together it might look like:

from dataclasses import dataclass

@dataclass
class Player: 
    firstname: str
    lastname: str
    coins: int

    def full_info(self) -> str:
        return f"{self.firstname} {self.lastname} {self.coins}"

    @classmethod
    def from_user_input(cls) -> 'Player':
        return cls(
            firstname=input("Please enter your first name:"),
            lastname=input("Please enter your second name: "),
            coins=100,
        )

Then you can call Player.from_user_input() to prompt the user for a name and return a new Player object:

>>> player = Player.from_user_input()
Please enter your first name:Bob
Please enter your second name: Small
>>> player
Player(firstname='Bob', lastname='Small', coins=100)
>>> player.full_info()
'Bob Small 100'
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your response, for player 1 I would do this: player1 = Player.from_user_input() Player(firstname='', lastname='', coins=100) print(player1) and then for the player 2 I would change player 1 to player 2?
I like this method, is it possible to simplyfy it a bit? Especially the part with with "str". In my case, it might be better to use more beggining approach
player1, player2 = Player.from_user_input(), Player.from_user_input() gets you 2 players. I'm not sure what you mean by "simplify" -- there might be some new concepts in here for you, but if you're a beginner the whole point of doing exercises like this is to learn things. :) If you encounter a concept that's confusing, it's more productive to ask "how does this work" than to ask "how do I do it without this" -- the other way might just be more complicated!
0

If you want to stay close to your original code, a few changes will work:

class Players(): 
    def __init__ (self, firstname, lastname, coins):   #initialising attributes
        self.firstname = firstname  
        self.lastname = lastname
        self.coins= coins
   
    def full_info(self):
      return self.firstname  + ' ' + self.lastname + ' ' + str(self.coins)
  
def get_user_input():
        firstname= input("Please enter your first name:")
        lastname= input ("Please enter your second name: ")
        coins= 100 #they are assigned automatically 
        return Players(firstname, lastname, coins)

An example:

a=get_user_input()

Please enter your first name:UN

Please enter your second name: Owen

a.full_info()
Out[21]: 'UN Owen 100'

5 Comments

Or just use self.firstname = input ... etc. No return required,
Thank you for your reposne! It did work. I have a question regarding other players. If person "a" is the first person, for the second person i would do the same but change a (to b, for exmaple)?
Yes; and if you want to do several (let's say n) players, you could use a list instead: n = number_of_players ; l_players = [] ; for i in range(n): l_players.append(get_user_input())
@user19077881: you're right, provided get_user_input() is defined as a method of the Players class; here I kept it an outside function (to stay close to the original code).
Hello; having to follow a link to outside code is kinda painful; please make this into a new question :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.