我正在创建一个创建冰淇淋的小程序。这真的很简单,但我看不出是什么搞砸了这个过程。我认为这是我缺乏函数和调用它们的经验。
基本上,它会随机创建一个冰淇淋......但我想在一个函数中调用一个函数并且它没有出现。它应该在使用 create_icecream 函数创建冰淇淋的味道和勺子后创建一个随机的勺量。
基本上,它会产生“是”或“否”并打印出来。
如果它随机选择“是”,则继续制作冰淇淋及其变量。
但是,我不确定我希望它列出它的顺序。所以将来如果我改变代码,我想在函数appear_icecream中决定它会先调用什么......即函数create_icecream()或consumer()只是在显示它时......我相信消费() 必须在 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
显然,勺数变量没有显示出来。