简体   繁体   English

如何为游戏中的玩家设置限制

[英]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.我有这个游戏,可以有无限数量的玩家,我想让它最少 2 个,最多 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我尝试将第 19 行到第 23 行替换为

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)预期的 output 是为了让玩家能够选择玩家数量(最少 2 人,最多 5 人)

If you insert 1 or any number above 5 it should say "please chose a number between 2 and 5.如果您插入 1 或任何大于 5 的数字,它应该说“请选择一个介于 2 和 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:)祝你好运:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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