0

我正在创建一个创建冰淇淋的小程序。这真的很简单,但我看不出是什么搞砸了这个过程。我认为这是我缺乏函数和调用它们的经验。

基本上,它会随机创建一个冰淇淋......但我想在一个函数中调用一个函数并且它没有出现。它应该在使用 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

显然,勺数变量没有显示出来。

4

3 回答 3

1

这也可以通过创建一个全局变量icecream_scoops_c并对其进行测试来解决。通过定义一个全局变量,我们可以确保所有函数都可以访问它们。从函数返回值也很有效。

在此处阅读全局变量的缺点 - 全局变量是否不好?

代码显示在这里。

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()
于 2013-04-22T19:10:49.087 回答
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代码块要简单得多。

于 2013-04-22T19:05:22.530 回答
1

勺子傻瓜的数量没有出现,因为您正在检查变量 icecream_scoops 根本没有改变。icecream_scoops 总是 ["One","Two","Three","Four"] 所以你的代码永远不会工作:D

看这里:

def create_icecream():
icecream_flavor_c = random.choice(icecream_flavor)
icecream_scoops_c = random.choice(icecream_scoops)

icecream_scoops_c 是存储随机选择的变量,因此这是您应该检查的变量。问题是,这是一个局部变量,所以你只能在函数内部访问它。所以使用它的方法是让你的 consumer() 函数使用参数。您可以从 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() 正在检查它。此外,当出现的结果为 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()
于 2013-04-22T18:55:40.287 回答