Python3,将函数中的字典值传递给另一个函数
我正在用PYTHON 3制作一个简单的骰子游戏Python3,将函数中的字典值传递给另一个函数,python,function,dictionary,Python,Function,Dictionary,我正在用PYTHON 3制作一个简单的骰子游戏 我想让游戏询问有多少玩家要玩,每个玩家从用户输入中得到自己的名字 我想让游戏询问每个玩家是否想要掷骰子 现在,我想从main函数中的dictionaryplayer\u dict到roll\u dice函数中获取值。因此,我可以打印出格式化字符串中的球员姓名值 提前谢谢,也很抱歉英语不好 from random import randint def main(): player = int(input('How many players
main
函数中的dictionaryplayer\u dict
到roll\u dice
函数中获取值。因此,我可以打印出格式化字符串中的球员姓名值
提前谢谢,也很抱歉英语不好
from random import randint
def main():
player = int(input('How many players> '))
step = 1
player += 1
player_dict = {}
for pl in range(1, player, step):
player_name = input(f'Player {str(pl)} name> ')
player_dict[pl] = player_name
for x in player_dict:
roll_dice()
def roll_dice():
start_rolling = input(f'{} roll dice? y/n> ') # I want to assign player_dict values in the formatted string
if start_rolling == 'y' or start_rolling == 'Y':
roll = randint(1, 6)
print(roll)
return roll_dice
main()
要获取
roll\u dice
函数中player\u dict
的值,可以将变量作为参数传递。甚至只传递要打印的值
就像这个例子:
从随机导入randint
def main():
player=int(输入('有多少玩家>'))
步骤=1
玩家+=1
player_dict={}
对于范围内的pl(1,播放器,步骤):
player_name=input(f'player{str(pl)}name>)
玩家名称
对于播放机目录中的x:
掷骰子(玩家掷骰子[x])
def掷骰子(玩家名称):
start_rolling=input(f'{player_name}roll dice?y/n>)#我想在格式化字符串中分配player_dict值
如果开始滚动=='y'或开始滚动=='y':
滚动=randint(1,6)
打印(卷)
返回骰子
main()
我决定将参数传递给另一个函数,并将所有内容嵌套在for循环下,看一看。此外,y/n选项还有改进的余地。但到目前为止,似乎还不错
坚持下去
从随机导入randint
def main():
player = int(input('How many players> '))
step = 1
player += 1
player_dict = {}
for pl in range(1, player, step):
player_name = input(f'Player {str(pl)} name> ')
player_dict[pl] = player_name
roll_dice(player_dict)
def roll_dice(players):
for i in range(1,len(players)+1):
start_rolling = input(f'{players[i]} roll dice? y/n> ') # I want to assign player_dict values in the formatted string
if start_rolling == 'y' or start_rolling == 'Y':
roll = randint(1, 6)
print(roll)
return roll_dice