我正在创建一个创建冰淇淋的小程序。这很简单,但我看不出是什么搞乱了这个过程。我认为这是我缺乏功能和调用它们的经验。
基本上,它会随机创建一个冰淇淋......但是我想在一个函数中调用一个函数而它没有出现。在创建具有create_icecream功能的冰淇淋的味道和勺子之后,它应该创造一个随机的一匙量。
基本上,它产生“是”或“否”并打印出来。
如果它随机选择“是”,则继续制作冰淇淋及其变量。
但是,我不确定我希望它列出的顺序。所以在将来,如果我改变代码,我想在函数shows_icecream中决定它首先调用什么...即函数create_icecream()或consume()首先显示它...我相信消费()必须在create_icecream()之后定义但仍在学习。
这是我到目前为止所写的内容:
import random
icecream_flavor = ["Chocolate","Vanilla","Strawberry"]
icecream_scoops = ["One","Two","Three","Four"]
icecream_made = ["yes","no"]
def create_icecream():
icecream_flavor_c = random.choice(icecream_flavor)
icecream_scoops_c = random.choice(icecream_scoops)
# Ice Cream Details: Printed
print("Scoops: ",icecream_scoops_c)
print("Flavor: ",icecream_flavor_c)
def consumption():
if icecream_scoops == "One":
icecream_spoonfuls = random.randint(0, 25)
print("Spoonfuls: ",icecream_spoonfuls)
elif icecream_scoops == "Two":
icecream_spoonfuls = random.randint(26, 50)
print("Spoonfuls: ",icecream_spoonfuls)
elif icecream_scoops == "Three":
icecream_spoonfuls = random.randint(51, 75)
print("Spoonfuls: ",icecream_spoonfuls)
elif icecream_scoops == "Four":
icecream_spoonfuls = random.randint(76, 100)
print("Spoonfuls: ",icecream_spoonfuls)
else:
return
def appear_icecream():
icecream_appear = random.choice(icecream_made)
# print(icecream_appear)
if str(icecream_appear) == "yes":
print("Yes")
create_icecream()
consumption()
elif str(icecream_appear) == "no":
print("No Ice Cream")
else:
print("BUG!")
print("Ice Cream No.1")
appear_icecream()
print("Ice Cream No.2")
appear_icecream()
运行示例:
Ice Cream No. 1
Yes
Scoops: One
Flavor: Strawberry
Ice Cream No. 2
No Ice Cream
显然,勺子变量的数量没有出现。
答案 0 :(得分:1)
勺子傻瓜的数量没有出现,因为你正在检查变量icecream_scoops,它根本没有变化。 icecream_scoops总是[“一个”,“两个”,“三个”,“四个”]所以你的代码永远不会起作用:D
见这里:
def create_icecream():
icecream_flavor_c = random.choice(icecream_flavor)
icecream_scoops_c = random.choice(icecream_scoops)
icecream_scoops_c是存储随机选择的那个,因此这是您要检查的变量。问题是,这是一个局部变量,因此您只能在函数内访问它。所以使用它的方法是让你的consumption()函数使用参数。你可以从create_icecream()
里面调用它 像这样:def create_icecream():
icecream_flavor_c = random.choice(icecream_flavor)
icecream_scoops_c = random.choice(icecream_scoops)
# Ice Cream Details: Printed
print("Scoops: ",icecream_scoops_c)
print("Flavor: ",icecream_flavor_c)
consumption(icecream_flavor_c)
def consumption(scoops):
if scoops == "One":
icecream_spoonfuls = random.randint(0, 25)
print("Spoonfuls: ",icecream_spoonfuls)
elif scoops == "Two":
icecream_spoonfuls = random.randint(26, 50)
print("Spoonfuls: ",icecream_spoonfuls)
elif scoops == "Three":
icecream_spoonfuls = random.randint(51, 75)
print("Spoonfuls: ",icecream_spoonfuls)
elif scoops == "Four":
icecream_spoonfuls = random.randint(76, 100)
print("Spoonfuls: ",icecream_spoonfuls)
else:
return
这样,你将值从icecream_scoops_c传递给consumtion(),因为var scoops和conmsumtion()正在检查它。此外,当来自appear_icecream()的结果为no时,您不会调用consumtion(),因此您正在那里赚取时间。
希望我很清楚,这有帮助!
以下完整代码:
import random
icecream_flavor = ["Chocolate","Vanilla","Strawberry"]
icecream_scoops = ["One","Two","Three","Four"]
icecream_made = ["yes","no"]
def create_icecream():
icecream_flavor_c = random.choice(icecream_flavor)
icecream_scoops_c = random.choice(icecream_scoops)
# Ice Cream Details: Printed
print("Scoops: ",icecream_scoops_c)
print("Flavor: ",icecream_flavor_c)
consumption(icecream_scoops_c) # call the function with the correct variable
def consumption(scoops): # added scoops arg
if scoops == "One":
icecream_spoonfuls = random.randint(0, 25)
print("Spoonfuls: ",icecream_spoonfuls)
elif scoops == "Two":
icecream_spoonfuls = random.randint(26, 50)
print("Spoonfuls: ",icecream_spoonfuls)
elif scoops == "Three":
icecream_spoonfuls = random.randint(51, 75)
print("Spoonfuls: ",icecream_spoonfuls)
elif scoops == "Four":
icecream_spoonfuls = random.randint(76, 100)
print("Spoonfuls: ",icecream_spoonfuls)
else:
return
def appear_icecream():
icecream_appear = random.choice(icecream_made)
# print(icecream_appear)
if str(icecream_appear) == "yes":
print("Yes")
create_icecream()
elif str(icecream_appear) == "no":
print("No Ice Cream")
else:
print("BUG!")
print("Ice Cream No.1")
appear_icecream()
print("Ice Cream No.2")
appear_icecream()
答案 1 :(得分:1)
主要问题是icecream_scoops
是list
类型的变量,因此永远不会等于字符串"One"
,"Two"
等。
在create_icecream()
中,您可以定义一个变量icecream_scoops_c
来保存勺子数量。很遗憾,icecream_scoops_c
是一个局部变量,无法在create_icecream()
之外访问。解决此问题的方法是return
此值:
def create_icecream():
# ...
# ...
return icecream_scoops_c
def consumption(num_scoops):
if num_scoops == "One":
# ...
elif num_scoops == "Two":
# ...
# ...
然后在你的主要代码中:
number_of_scoops = create_icecream()
consumption(number_of_scoops)
顺便说一句,最好将ice_cream_scoops
保留为数字列表([1, 2, 3, 4]
而不是["One", "Two", "Three", "Four"]
)。这样,在consumption()
中,您可以执行以下操作:
def consumption(num_scoops):
spoonfuls = random.randint((num_scoops - 1) * 25 + 1, num_scoops * 25)
print("Spoonfuls:\t", spoonfuls)
这比整个if...elif
代码块简单得多。
答案 2 :(得分:1)
这也可以通过创建全局变量icecream_scoops_c
并对其进行测试来解决。
通过定义全局变量,我们可以确保所有函数都可以访问它们。
从函数中返回值也很有效。
在这里阅读全局变量的缺点 - Are global variables bad?
此处显示代码。
import random
icecream_flavor = ["Chocolate","Vanilla","Strawberry"]
icecream_scoops = ["One","Two","Three","Four"]
icecream_made = ["yes","no"]
icecream_scoops_c = None
def create_icecream():
global icecream_scoops_c
icecream_flavor_c = random.choice(icecream_flavor)
icecream_scoops_c = random.choice(icecream_scoops)
# Ice Cream Details: Printed
print("Scoops: ",icecream_scoops_c)
print("Flavor: ",icecream_flavor_c)
def consumption():
global icecream_scoops_c
if icecream_scoops_c == "One":
icecream_spoonfuls = random.randint(0, 25)
print("Spoonfuls: ",icecream_spoonfuls)
elif icecream_scoops_c == "Two":
icecream_spoonfuls = random.randint(26, 50)
print("Spoonfuls: ",icecream_spoonfuls)
elif icecream_scoops_c == "Three":
icecream_spoonfuls = random.randint(51, 75)
print("Spoonfuls: ",icecream_spoonfuls)
elif icecream_scoops_c == "Four":
icecream_spoonfuls = random.randint(76, 100)
print("Spoonfuls: ",icecream_spoonfuls)
else:
return
def appear_icecream():
icecream_appear = random.choice(icecream_made)
# print(icecream_appear)
if str(icecream_appear) == "yes":
print("Yes")
create_icecream()
consumption()
elif str(icecream_appear) == "no":
print("No Ice Cream")
else:
print("BUG!")
print("Ice Cream No.1")
appear_icecream()
print("Ice Cream No.2")
appear_icecream()