python - 有没有一种简单的方法可以将字典带入函数中?
问题描述
我们的目标是用这个来玩掷骰子。
我创建了一个字典“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)```
解决方案
您可以完全将其作为参数丢弃。
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
推荐阅读
- angular - Angular - 错误:初始超出最大预算。预算 5.00 MB 未达到 197.06 kB 总共 5.19 MB
- blockchain - 如何为初创公司创建加密钱包(比特币和以太坊)?
- javascript - 将音频播放器附加到按钮
- npm - npm 运行构建缺少的文件 - vitejs
- javascript - 如何组合 2 个 JSON 对象?
- flutter - Dart/Flutter 中的编码需要我的第一个 Tile 只出现一次
- javascript - 如何在nodejs中过滤多级类别
- apache - 在 apache / wsgi 下运行 python/诗歌烧瓶应用程序时遇到问题
- python - 通过具有小缓冲区大小和“while True...break”的套接字进行客户端-服务器数据交换:客户端 OK。服务器端卡住
- javascript - JQuery和AJax用于同一类的多个提交按钮而不刷新页面?