【发布时间】:2020-10-10 10:34:00
【问题描述】:
我正在尝试引用 function_one 中的字典。我试图返回字典变量,并将字典名称用作参数和参数。但是,我收到一条错误消息,指出我试图在 function_two 中访问的字典未定义。
这是我的简化代码:
def function_one():
first_dictionary = {"text1": "text2","text3": "text4"}
second_dictionary = {"example1": "example2","example3": "example4"}
for i in first_dictionary:
print(i,first_dictionary[i])
for i in second_dictionary:
print(i,second_dictionary[i])
return first_dictionary,second_dictionary
def function_two(first_dictionary,second_dictionary):
total_cost = 0
input1 = True
while input1 != '0':
input1 = input("Input1")
input2 = int(input("Input2".format(input1)))
if input1 in first_dictionary:
total_cost += input2 * 5
elif input1 in second_dictionary:
total_cost += input2 * 4
#main Routine
function_one()
function_two(first_dictionary,second_dictionary)
基本上,我是在询问为 input1 选择的元素是否在上一个函数的字典中,我希望程序更改 total_cost 值等。
【问题讨论】:
-
要么使用返回值、全局变量或包含这些字典的类。就目前而言,您没有对来自
function_one()的返回值做任何事情。
标签: python function dictionary parameters arguments