[英]How to put a limit for the player in the game
我有這個游戲,可以有無限數量的玩家,我想讓它最少 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])
我嘗試將第 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:
預期的 output 是為了讓玩家能夠選擇玩家數量(最少 2 人,最多 5 人)
如果您插入 1 或任何大於 5 的數字,它應該說“請選擇一個介於 2 和 5 之間的數字。
你可以這樣寫:
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: "))
祝你好運:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.