1

I have the below code where I am interested in using the fqdm_or_ip variable in another python file.

This file is menu.py

def CONN_PARAMS():
    print("1. FQDN")
    print("2. IP ADDRESS")
    fqdn_or_ip = input("Are you going to use an IP or FQDN for the controller")
    if fqdn_or_ip == 1:
        controller = input("Enter FQDN or IP address"  )
        print("Are you sure this is correct?   " + controller)

    elseif (fqdn_or_ip) == 2):
        controller = input("Enter FQDN or IP address"):

I am using the below code but I am unsure how to move the variable between files...once the menu.py collects answers to my questions the other python file will begin using that information.

This file is the one I am trying to pull the variable into....I think I am supposed to pass the variable into the () of menu.CONN_PARAMS but I was unsure how.

#!/usr/bin/python3

# import requests module for REST API calls
import requests

# import json module
import json

# menu.py
import menu

menu.CONN_PARAMS()

Best,

Dan

3
  • Your code seems to be syntactically incorrect. Did you run it? Also, passing globals around like hand-me-downs is a bad idea. You should consider returning those values from the function. Commented Jul 3, 2017 at 21:58
  • 1
    fqdn_or_ip seem to be a local variable. Can you pass the variable as an argument to some other function? Commented Jul 3, 2017 at 21:59
  • @cᴏʟᴅsᴘᴇᴇᴅ May be a moment I am designing it incorrectly because of my lack of experience. I wanted to separate all the user interaction ...input ...from the part of the program that will use that data. Menu would lead the user to answer questions and another file would act on the answers...sounds like that may be a bad idea. Menu collects data and another file would perform rest API functions. Commented Jul 4, 2017 at 22:18

1 Answer 1

3

You could return the value you're interested in from the function you're calling.

If you're interested in multiple values you could also return a tuple / list / dict / ... instead of a single value.

menu.py

def CONN_PARAMS():
    print("1. FQDN")
    print("2. IP ADDRESS")
    fqdn_or_ip = input("Are you going to use an IP or FQDN for the controller")
    # ... whatever you need to do
    return fqdn_or_ip

other_file.py

# menu.py
import menu

fqdn_or_ip = menu.CONN_PARAMS()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.