Can someone please tell me whats wrong with this code i wrote myself in python


#1
total = 0
alto = 0

list_fruits = {
"banana" : 4, 
"apple" : 6,         
"pomogranates" : 7,        
"strawberry" : 7,       
"blueberry" : 2        
} 

fruits_prices = {
"banana" : 10.5, 
"apple" : 5.0,         
"pomogranates" : 15,        
"strawberry" : 20.5,       
"blueberry" : 25.0        
 }

fruits_stock = {
"banana" : 11.0, 
"apple" : 5.0,         
"pomogranates" : 7.0,        
"strawberry" : 20.0,       
"blueberry" : 0.0        
 }

def cost_fruits(fruits):
    total = 0
    alto = 0
        
    for key in fruits :
        if fruits_stock[key] > 0 :
            total = total + fruits_prices[key]
            fruits_stock[key] -= list_fruits[key]
            alto += list_fruits[key]          
                
        else :
            print ("The fruit is not in stock please choose another")
    
    return total, alto         
cost_fruits(list_fruits)
result = 0
result = total * alto
print ("the totla price is ",result)

#2

to many declarations of variable, you declare both total and alto twice, why?

you do nothing with the returned result by the function, why?


#3

it was giving an error that total and alto was undefined and it was used before assignment


#4

but now you defined them globally (line 1 and 2) and locally (in the function), that is a bit double, isn’t it? Which one should it be?


#5

the local one with in the cost_fruits function


#6

that is part 1, part 2 was this:


#7

what do you mean i used them in the second last line “result = total * alto”


#8

yes, but where did you define these variables?
So what scope do these variables have?

If you answer these questions correctly, you should see your problem

this is why you return these variable, but then you call the function, and do nothing with what the function returns


#9

I first defined them locally i.e with in the function
I don’t quite understand your second question “what scope do these variables have?”


#10

if you define variable locally, they have a local scope.

So these variables only exist within the function, how can you use them outside the function then?


#11

okay i finally understood my problem i was return local variables in which they were returning zero thus my calculation was printing out zero so my code inside the cost_fruits(fruits) function should be like this :

def cost_fruits(fruits):
    total = 0
    alto = 0
    result = 0
    
    for key in fruits :
        if fruits_stock[key] > 0 :
            total = total + fruits_prices[key]
            fruits_stock[key] -= list_fruits[key]
            alto += list_fruits[key]          
            result = total*alto    
            
        else :
            print ("The fruit is not in stock please choose another")
    
    return print ("the total price is ",result)

#12

and the last line should be like :
print (cost_fruits(list_fruits))


#13

this is a bit weird:

return print ("the total price is ",result)

print and return at the same line, which one is it?

its one of the possible solutions, its not the only solution.


#14

What do you think is the best solution to my problem ?


#15

wait can i do it like this :
def cost_fruits(fruits):
total = 0
alto = 0
result = 0

for key in fruits :
    if fruits_stock[key] > 0 :
        total = total + fruits_prices[key]
        fruits_stock[key] -= list_fruits[key]
        alto += list_fruits[key]          
        result = total*alto    
        
    else :
        print ("The fruit is not in stock please choose another")

return result         

print ("the total price is ", cost_fruits(list_fruits))


#16

i would do the last one you did, return the result and then print the returned result at function call


#17

okay thank you so much you have been a great help stetim94