简体   繁体   中英

How to put a limit for the player in the game

I have this game, there could be unlimited amount of players, I want to make it minimum 2 and maximum 5.

from dataclasses import dataclass
@dataclass
class Player: 
    firstname: str
    lastname: str
    coins: int
    slot: int
    def full_info(self) -> str:
        return f"{self.firstname} {self.lastname} {self.coins} {self.slot}"

    @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,
            slot= 0)
    
n = int(input("Number of players:"))
playersingame = []
for i in range(n):
    playersingame.append(Player.from_user_input())


print([player.full_info() for player in playersingame])

I tried replacing lines 19 to 23 to

max_players= 0
while (max_players <2) or (max_players > 5) :
    max_players = int(input(" Please choose a number of players between 2 and 5.  "))
    
while len (players_dict) < max_players: 

The expected output is to enable the player to choose the amount of players ( minimum 2 and maximum 5)

If you insert 1 or any number above 5 it should say "please chose a number between 2 and 5.

You can write something like this:

number_of_players = int(input("Number of players:"))
while not (2 <= number_of_players <= 5):
    print("please chose a number between 2 and 5")
    number_of_players = int(input("Number of players: "))

Good luck:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM