Calling an output from a function from a different file?

user155876

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.

Anshu Dwibhashi

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

The whole package:

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Calling a function from a different react file

From Dev

Dynamic Function Calling From a Different File - Python

From Dev

Avoiding multiple import in Kivy when calling a function from a different file

From Dev

How to customise a shiny plotting function to output different plots depending on the parameters received from calling the function

From Dev

Function Output Format when calling from a dictionary

From Dev

Calling function from different threads is blocking?

From Dev

Calling java script function from different tags

From Dev

Calling Function From a different Class in Android Studio

From Dev

calling a function from two different pages

From Dev

How is injecting an impure function different from calling it?

From Dev

Calling a function in a .cpp file from a .c file

From Dev

Calling text from a different php file

From Dev

Creating a file from a output function

From Dev

Write on a file from a function - No output

From Dev

Errno 2 file not found when calling a function from a different .py file

From Dev

PHP calling function from another file not working

From

Calling a function from another file fails

From Dev

Calling a function from HTML to Python file

From Dev

Calling Dispatch function from a blank javascript file

From Dev

Calling an assembly function from another file

From Dev

Calling function in main file from component

From Dev

Octave calling function from another m file

From Dev

Calling ViewController function from another .swift file

From Dev

Calling a template function from a C file

From Dev

Calling a function from another file to use in templates

From Dev

Python: calling function from imported file

From Dev

Calling a Python function from another file

From Dev

Python Calling Function from Another File

From Dev

Calling a javascript function from other file

Related Related

  1. 1

    Calling a function from a different react file

  2. 2

    Dynamic Function Calling From a Different File - Python

  3. 3

    Avoiding multiple import in Kivy when calling a function from a different file

  4. 4

    How to customise a shiny plotting function to output different plots depending on the parameters received from calling the function

  5. 5

    Function Output Format when calling from a dictionary

  6. 6

    Calling function from different threads is blocking?

  7. 7

    Calling java script function from different tags

  8. 8

    Calling Function From a different Class in Android Studio

  9. 9

    calling a function from two different pages

  10. 10

    How is injecting an impure function different from calling it?

  11. 11

    Calling a function in a .cpp file from a .c file

  12. 12

    Calling text from a different php file

  13. 13

    Creating a file from a output function

  14. 14

    Write on a file from a function - No output

  15. 15

    Errno 2 file not found when calling a function from a different .py file

  16. 16

    PHP calling function from another file not working

  17. 17

    Calling a function from another file fails

  18. 18

    Calling a function from HTML to Python file

  19. 19

    Calling Dispatch function from a blank javascript file

  20. 20

    Calling an assembly function from another file

  21. 21

    Calling function in main file from component

  22. 22

    Octave calling function from another m file

  23. 23

    Calling ViewController function from another .swift file

  24. 24

    Calling a template function from a C file

  25. 25

    Calling a function from another file to use in templates

  26. 26

    Python: calling function from imported file

  27. 27

    Calling a Python function from another file

  28. 28

    Python Calling Function from Another File

  29. 29

    Calling a javascript function from other file

HotTag

Archive