I created a file
#name file ask.py
def ask_name():
user = input("What is your name?: ")
and i imported it
import ask
ask.ask_name()
print(user)
but python tells me that "user" is not defined.
I created a file
#name file ask.py
def ask_name():
user = input("What is your name?: ")
and i imported it
import ask
ask.ask_name()
print(user)
but python tells me that "user" is not defined.
It's not: user is a variable internal to ask_name, not an attribute of ask. Try this:
#name file ask.py
def ask_name():
user = input("What is your name?: ")
return user
# Main file
import ask
local_input = ask.ask_name()
print(local_input)
the user
variable "lives" only inside the 'ask_name` function (read this excellent post to learn more about variable scope), so it's undefined within the main script.
you can have the function return the user's input, and print that returned value.
#name file ask.py
def ask_name():
return input("What is your name?: ")
# Main file
import ask
print(ask.ask_name())