首页 > 解决方案 > 有没有一种简单的方法可以将字典带入函数中?

问题描述

我们的目标是用这个来玩掷骰子。

我创建了一个字典“all_dice”,最终在使用它之前将它传递给了 3 个函数。

必须有一种更简单的方法来访问字典,而不必像这样一遍又一遍地传递它。

import random
from random import randint

x = 0
the_dice_chosen = ""

##this is the dictionary

all_dice = {"six": 
        [["_____",],
        ["0   0",],
        ["0   0",],
        ["0   0",],
        ["_____",],],
        "five": 
        [["_____",],
        ["0   0",],
        ["  0  ",],
        ["0   0",],
        ["_____",],],
        "four": 
        [["_____",],
        ["0   0",],
        ["     ",],
        ["0   0",],
        ["_____",],],
        "three": 
        [["_____",],
        ["0    ",],
        ["  0  ",],
        ["    0",],
        ["_____",],],
        "two": 
        [["_____",],
        ["0    ",],
        ["    ",],
        ["    0",],
        ["_____",],],
        "one": 
        [["_____",],
        ["     ",],
        ["  0  ",],
        ["     ",],
        ["_____",],],}

## This prints the dictionary of the numbers chosen side by side.
## Again "all_dice" is passed as "ad" and is used.

def dice_print(ndy,ndz,ad):
    x = 0
    nday = ad[ndy]
    ndaz = ad[ndz]
    for i in nday:
        print(nday[x], ndaz[x])
        x = x + 1

##This creates the random dice numbers.       

def dice_roller():
    x = randint(1, 6) 
    y = randint(1, 6) 
    return(x, y)

## This converts the numbers into selections in the dictionary. E.G. 6 into "six" "all_dice" is ad
## and it will from now on be passed as "ad".

def dice_maker(ad,):
    master = {1 : "one",
              2 : "two",
              3 : "three",
              4 : "four",
              5 : "five",
              6 : "six",}
    for i in range(1,2):
        x = dice_roller()
        y = int(x[0])
        z = int(x[1])
        new_die_y = (master[y])
        new_die_z = (master[z])
        dice_print(new_die_y,new_die_z,ad)


##This calls the script to action and passing the dictionary "all_dice"

dice_maker(all_dice)```

标签: pythonpython-3.xfunctiondictionary

解决方案


您可以完全将其作为参数丢弃。

python 模块的行为非常类似于单例类。一个模块,就像一个类,是一个对象。all_dice是模块的一个属性,因此可以被同一模块范围内的函数访问。

import random
from random import randint

x = 0
the_dice_chosen = ""

##this is the dictionary

all_dice = {"six": 
        [["_____",],
        ["0   0",],
        ["0   0",],
        ["0   0",],
        ["_____",],],
        "five": 
        [["_____",],
        ["0   0",],
        ["  0  ",],
        ["0   0",],
        ["_____",],],
        "four": 
        [["_____",],
        ["0   0",],
        ["     ",],
        ["0   0",],
        ["_____",],],
        "three": 
        [["_____",],
        ["0    ",],
        ["  0  ",],
        ["    0",],
        ["_____",],],
        "two": 
        [["_____",],
        ["0    ",],
        ["    ",],
        ["    0",],
        ["_____",],],
        "one": 
        [["_____",],
        ["     ",],
        ["  0  ",],
        ["     ",],
        ["_____",],],}

## This prints the dictionary of the numbers chosen side by side.
## Again "all_dice" is passed as "ad" and is used.

def dice_print(ndy,ndz):
    x = 0
    nday = all_dice[ndy]
    ndaz = all_dice[ndz]
    for i in nday:
        print(nday[x], ndaz[x])
        x = x + 1

##This creates the random dice numbers.       

def dice_roller():
    x = randint(1, 6) 
    y = randint(1, 6) 
    return(x, y)

## This converts the numbers into selections in the dictionary. E.G. 6 into "six" "all_dice" is ad
## and it will from now on be passed as "ad".

def dice_maker():
    master = {1 : "one",
              2 : "two",
              3 : "three",
              4 : "four",
              5 : "five",
              6 : "six",}
    for i in range(1,2):
        x = dice_roller()
        y = int(x[0])
        z = int(x[1])
        new_die_y = (master[y])
        new_die_z = (master[z])
        dice_print(new_die_y,new_die_z)


##This calls the script to action and passing the dictionary "all_dice"

dice_maker()

global关键字_

如果您需要all_dice在函数中重新分配。您必须添加global all_dice到该功能。

all_dice = None

def set_all_dice(value):
    global all_dice
    all_dice = value

def _set_all_dice(value):
    all_dice = value       ## all_dice here is scoped to the function

推荐阅读