0

I want to assign a variable in one file with it's own code. Import it into another one and use the variable.

What i was trying to do was get a username in one script and use it in another. Here's what I did:

Intro.py

def Intro():
    print("Welcome to *game name here*!")
    username = input("What is your username?")

Main.py

import Intro
Intro.Intro()
print(username)
Hungry
  • 1
  • 2

1 Answers1

0

You can't do it that way, because username is a local variable inside Intro and is not visible anywhere else.

The usual solution to this problem is to return the value:

def Intro():
    print("Welcome to *game name here*!")
    username = input("What is your username?")
    return username

username = Intro.Intro()
John Gordon
  • 29,573
  • 7
  • 33
  • 58