下面是我的Python3.4代码的一部分。它完全符合我的要求,但我希望返回变量“配料”和字典中的键"lemon_drizzle“,这样我就可以在函数外部使用它们。
how_lemon= int(input('How many lemon drizzle cakes would you like to
bake?'))
#list of the ingredients for cakes
lemon_drizzle={
'Double Cream':140,
'Lemon Zest':3,
'Salt':1,
'Baking Power':0.5}
plain_flour2 = lemon_drizzle['Plain Flour']= 120
sugar2 = lemon_drizzle['Sugar']= 140
unsalted_butter2 = lemon_drizzle['Unsalted Butter']= 40
free_eggs2 = lemon_drizzle['Free Range Eggs']= 1
def function_ingredients(cake, how_many):
for key,val in cake.items():
ingredients = val * how_many
'''
I want this to return 'ingredients' and all the keys
so that it does exactly the same this, but outside the function.
'''
print(ingredients, key)
print('\nFor your lemon drizzle cakes you will need:\n')
lemon_ingredients = function_ingredients(lemon_drizzle, how_lemon)感谢您的收听,希望您能回答我的问题!附言:这是一个非常简单的问题!
发布于 2015-05-04 04:24:07
这是一个缩进问题。试试这个:
def function_ingredients(cake, how_many):
for key,val in cake.items():
ingredients = val * how_many
# I want this to return 'ingredients' and all the keys
# so that it does exactly the same this, but outside the function.
print(ingredients, key)我还用Python注释替换了您的多行字符串文字(带有三重引号的字符串)。如果您确实需要多行字符串,请小心正确地缩进它。
也是
plain_flour2 = lemon_drizzle['Plain Flour']= 120可以替换为:
lemon_drizzle['Plain Flour']= 120https://stackoverflow.com/questions/30017081
复制相似问题