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.
-
Create your minimal reproducible example and add to your question,PM 77-1– PM 77-12022年12月02日 22:54:40 +00:00Commented 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 timeSwifty– Swifty2022年12月02日 22:57:04 +00:00Commented Dec 2, 2022 at 22:57
2 Answers 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'
3 Comments
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!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
self.firstname = input ... etc. No return required,