user19355475
user19355475

Reputation:

How To Call a Function from a Function in a diffrent file then return from the funtion you called to the function where you called it? python

My titles are pretty much my question (How To Call a Function from a Function in a different file then return from the function you called to the function where you called it?)

I've been trying to find an answer for this for a while here's my code:

module 1:

from module2 import store
def car():
     input = input("where would you like to drive 1: store 2: Park")
     store() ## for the sake of time ill just call this instead of writing an

module 2:

def store():
      input2 = input("What do you want to do go to 1: buy stuff 2: go back to the car")
      if input2 == "1":
          return
      else:
          ### buy stuff

Can I use a return statement with no parameters to quit the function entirely and not run the buy stuff code but then go back and continue running my car function, if the user wanted to go back to their car, or is there a better method?

Upvotes: 0

Views: 58

Answers (1)

Feline
Feline

Reputation: 784

Response to the new part in OP's edited question:

Yes, you can always have a bare return statement for a function. The function in this case will return None when the condition is not met.


If I understand your question correctly, you're asking:

How to import then call a function from another module.

Here's a simple example on how to do it:

# file_1.py
def greet(name):
  return f'Hello, {name}!'
# file_2.py
from file_1 import greet
print(greet('World'))
# 'Hello, World!'

For you specific example, you can use the information above and do something like this:

# module1.py
from module2 import store
def car():
     input_1 = input("where would you like to drive 1: store 2: Park")
     input_2 = store()
     # then do whatever ....
# module2.py
def store():
      ans = input(
    "What do you want to do go to 1: buy stuff 2: go back to the car")
      if ans == "2":
          return
      else:
          ### buy stuff

Upvotes: 4

Related Questions