Home > Software design >  Using user input function in Classes
Using user input function in Classes

Time:12-03

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.

CodePudding user response:

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'

CodePudding user response:

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'
  • Related