我想查一下功能一中的词典。我尝试返回字典变量,并使用字典名称作为参数和参数。但是,我收到一条错误消息,说我试图在函数\ 2中访问的字典没有定义
以下是我的简化代码:
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选择的元素是否在上一个函数的字典中,我希望程序更改总成本值等
首先需要从函数_one()返回值。 您可以执行以下操作:
否则,您可以使用在大多数情况下不建议使用的全局变量
您没有获得返回值:
调用
function_one()
时,没有使用它返回的词典。 您可以使用此解决您的问题:相关问题 更多 >
编程相关推荐