我尝试创建掷骰子。
首先,玩家选择玩家人数:
def main():
print("Welcome.")
while True:
try:
number_of_players = int(input("\nEnter the number of players: "))
break
except ValueError:
print("\nEnter the correct number of players.")
throw(number_of_players)
我有几类球员
class Player:
def __init__(self, number_of_player):
self.number_of_player = number_of_player
self.number_of_throws = 0
def add_throw(self):
self.number_of_throws += 1
def number_of_throws(self):
print(self.number_of_throws)
def number_of_player(self):
print(self.number_of_player)
骰子类:
class Dice:
def random_number(self):
print(randint(1, 6))
有throw()
函数:
def throw(number_of_players):
dice = Dice()
for number_of_player in range(pocet_hracu):
player[number_of_player] = Player(number_of_player)
random_number = dice.random_number()
print("Player {} threw {}.".format(player[number_of_player].number_of_player(), random_number)
问题出在throw()
函数中。
我需要将变量设置为player[0] = Player(0)
... player[number_of_player] = Player(number_of_player)
。
但是为什么?