0

I am creating a little program that creates Ice Cream. It's really simple but I cannot see what is messing up the process. I assume it is my lack of experience with functions and calling on them.

Basically, it creates an ice cream randomly...but I would like to call a function within a function and it is not appearing. It should create a random spoonful amount after creating the flavor and scoops of ice cream with the function of create_icecream.

Basically, it Produces a "Yes" or "No" and prints that.

If it randomly selects "Yes", it proceeds with making the ice cream and its variables.

However, I am not sure the order I want it to list it. So in the future, if I change the code, I would like to decide in the function appear_icecream, what it will call first...ie the function create_icecream() or consumption() first just when displaying it...I believe consumption() has to be defined after create_icecream() but still learning.

Here is what I have written so far:

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()

An Example of the Run:

Ice Cream No. 1    
Yes
Scoops:    One
Flavor:    Strawberry
Ice Cream No. 2
No Ice Cream

Obviously, the number of spoonfuls variable is not showing up.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222

3 Answers3

1

The number of spoonfools is not appearing because you are checking the variable icecream_scoops which didn't change at all. icecream_scoops is always ["One","Two","Three","Four"] so your code is never gonna work :D

See in here:

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

icecream_scoops_c is the one storing the random choice so that is the variable you ougth to check. The problem is, that is a local variable so you only have access to it inside the function. So the way to use it is having your consumption() function use arguments. And you can call it from inside create_icecream()

like this:

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

That way, you are passing the value from icecream_scoops_c to consumtion() as the var scoops and conmsumtion() is checking it. Also you don't call consumtion() when the result from appear_icecream() is no, so you are earning time in there.

Hope I was clear and that it helps!

Full code below:

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()
Itxaka
  • 767
  • 6
  • 10
1

The main problem is that icecream_scoops is a variable of type list, so will never be equal to the strings "One", "Two", etc.

In create_icecream(), you define a variable icecream_scoops_c to hold the number of scoops. Unfortunately, icecream_scoops_c is a local variable and is not accessible outside of create_icecream(). The way to fix this is to return this value:

def create_icecream():
    # ...
    # ...
    return icecream_scoops_c

def consumption(num_scoops):
    if num_scoops == "One":
        # ...
    elif num_scoops == "Two":
        # ...
    # ...

Then in your main code:

number_of_scoops = create_icecream()
consumption(number_of_scoops)

Incidentally, it's probably better to keep ice_cream_scoops as a list of numbers ([1, 2, 3, 4] instead of ["One", "Two", "Three", "Four"]). That way, in consumption(), you could do the following:

def consumption(num_scoops):
    spoonfuls = random.randint((num_scoops - 1) * 25 + 1, num_scoops * 25)
    print("Spoonfuls:\t", spoonfuls)

Which is much simpler than that whole if...elif code block.

Joel Cornett
  • 24,192
  • 9
  • 66
  • 88
1

This can also be solved by making a global variable icecream_scoops_c and testing for it. By defining a global variable, we can make sure that all the functions have access to them. Returning values from functions works great too.

Read the cons of global variables here - Are global variables bad?

Code is shown here.

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()
Community
  • 1
  • 1
Sukrit Kalra
  • 33,167
  • 7
  • 69
  • 71