This is a very rudimentary question.
I have a program that calculates the total number of fruit (apples + bananas) based on the number of days passed, which is input by the user. The apple production and total fruit count is calculated in fruit.py:
from bananas import Bp
day_count = int(input("How many days have passed?" ))
apple_production = day_count * 100
fruit_total = apple_production + banana_production
print("Total amount of fruit is", fruit_total)
The banana production is calculated in a different file bananas.py and defined as a function:
def Bp(day_count):
banana_production = day_count * 200
return banana_production
So my issue is that when I try to run bananas.py, I get the error "name 'banana_production' is not defined".
Obviously, I'm missing an important step here. How do I call the output value for banana_production from bananas.py to use in fruit.py?
By the way, I realize that I could merge both into a single file but the whole point of this question is to find out how to do it this way.
You get the message banana_production is not defined because you haven't defined it anywhere in fruit.py. Though the variable is defined in bananas.py you'll have to do something like this to achieve what you want:
So, all you have to do is add this line:
banana_production = Bp(day_count)
after you calculate apple_production
bananas.py: (No change):
def Bp(day_count):
banana_production = day_count * 200
return banana_production
fruit.py:
from bananas import Bp
day_count = int(input("How many days have passed?" ))
apple_production = day_count * 100
banana_production = Bp(day_count) # Add this line
fruit_total = apple_production + banana_production
print("Total amount of fruit is", fruit_total)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments