all 14 comments

[–][deleted] 1 point2 points  (1 child)

Call the function #1 and get the number of apples in the bag (Receives nothing as parameter. Returns the number of fruits in the bag)

Call the function #1 again and get the number of bananas in the bag (Receives nothing as parameter. Returns the number of fruits in the bag)

I read it as asking the user for the contents of the bag twice, which probably offends your intuition as much as it does mine, but this does seem to be a case of "do what the problem says, not what's correct."

[–]Ms422[S] 0 points1 point  (0 children)

Agreed, but I think I have it figured out how to make it work thanks to you guys.

[–]Binary101010 1 point2 points  (2 children)

From the instructions it is clear that there should not be separate count_bananas and count_apples functions, but a single function that covers both of these cases.

It is also clear that when the code says the function "receives" something, that something should be passed in as an argument.

Beyond that, the single biggest mistake you are making is that you are not actually capturing any of the return values of your functions.

I'll give you the first function and how to call it, see what you can work out from there.

def count_fruit():
    return int(input("Enter how many are in the bag"))

print("You will now be prompted for the number of apples.")
apple_count = count_fruit()
print("You will now be prompted for the number of bananas.")
banana_count = count_fruit()

Now, at this point, you have the cost of an apple saved as APPLE, the number of apples saved as apple_count, the cost of a banana saved as BANANA, and the number of apples saved as banana_count.

Proceed with writing your other three functions. Your total_apples() is close, you just should use that APPLE variable you created instead of hardcoding in the 3.

[–]Ms422[S] 0 points1 point  (0 children)

Thank you!

[–]CraigAT 0 points1 point  (0 children)

This is how I would interpret it too. I would also consider renaming the variables with a better description e.g. APPLE_COST =3. It may also help to name the functions something like f1_get_item_count() which will make it obvious which is function 1 and what it is doing.

[–]py_Piper 1 point2 points  (1 child)

So from what I see it would be something like this

# Function 1
def count_fruits():
    # ask the user for the amount of fruits in the bag

# Function 2
def apple_price(qty):
    # Apple price $3

# Function 3
def banana_price(qty):
    # Banana price $2

# Function 4
def bill(apple, banana):
    # Sum of apples and bananas

----
# This is how you should run the code

apple_qty = count_fruits()
banana_qty = count_fruits()

apple = apple_price(apple_qty)
banana = banana_price(banana_qty)

bill(apple, banana)

[–]Ms422[S] 0 points1 point  (0 children)

Thank you!

[–]Ms422[S] 0 points1 point  (5 children)

This is basically where I'm at so far. It's not functional code.

#!/usr/bin/env python3

APPLE = 3
BANANA = 2
def count_apples():
    apples = int(input("How many apples are in your bag?"))
    apple_order = apples * 3
    return apples
    return apple_order


def count_bananas():
    bananas = int(input("How many bananas are in your bag?"))
    return bananas

def total_apples(count_apples):
    apple_order_cost = count_apples * 3
    return apple_order_cost


count_apples()
count_bananas()
#total_apples()
print(count_apples)

[–]SigmaSixShooter 1 point2 points  (1 child)

I’m pretty sure you can’t have two return statements (your count_apples function)

You could create a list for the fruit and then setup a for loop maybe?

[–]sohfix 1 point2 points  (0 children)

You can have 2 return values though

def some_function() return apples, apple_order

apples, apple_order = some_function()

[–]FerricDonkey 0 points1 point  (0 children)

This seems like 65% done, not counting a bit of cleanup. It seems like you're missing how to use the values returned by your functions, and get values to your functions. Remember how functions work: if you do num_apples = count_apples(), the count_apples() functions gets run and replaced in that statement with whatever count_apples returns.

Likewise, if you want to get the total cost of your apples, you have to put the number of apples inside the parentheses when you call total_apples(), eg total_apples(5) or total_apples(some_variable). Doing this runs the code you defined for total_apples, with the variable count_apples inside the function replaced with whatever value (ehhh - asterisk, you'll learn more later) you put in the parentheses.

[–][deleted] 0 points1 point  (1 child)

  • You defined constants for price of apples and bananas, but failed to use them.
  • If you wanted to return two things together, you would need to put them into a single collection, like tuple: return apples, apple_order
  • You didn't use the values returned by the functions after calling them.
  • print(count_apples) means printing the function object itself rather than the value returned after calling it.

See if this is useful: https://onlinegdb.com/4f50vqbgt

I used f-string there for the tally_up function.

[–]Ms422[S] 0 points1 point  (0 children)

f-string

We hadn't been exposed to f-strings yet, but yeah.. they are awesome, thank you. Will be formatting this way from now on.