2

So, I have this stuff below

def userinput():
    adjective1 = input("Adjective: ")
    noun1 = input("Noun: ")
    noun2 = input("Noun: ")

def story():
    print("A vacation is when you take a trip to some " + adjective1 + " place.")
    print("Usually you go to some place that is near " + noun1 + " or up on " + noun2 + ".")

then when I run the functions and provide input, it comes back with

 File "/Users/apple/Dropbox/MadLibs 6.py", line 52, in story
print("A vacation is when you take a trip to some " + adjective1 + " place with your "+ adjective2 + " family.")
NameError: name 'adjective1' is not defined

What does it mean by this, and how can I fix it?

1
  • 1
    you need to set the two variable as global. Commented Nov 8, 2015 at 15:52

3 Answers 3

7

Its all about scope, you can not acces variable within another function scope Try this:

def userinput():
    adjective1 = input("Adjective: ")
    noun1 = input("Noun: ")
    noun2 = input("Noun: ")
    return adjective1, noun1, noun2

def story():
    adjective1, noun1, noun2 = userinput()
    print("A vacation is when you take a trip to some " + adjective1 + " place.")
    print("Usually you go to some place that is near " + noun1 + " or up on " + noun2 + ".")

By calling userinput on the second function and getting its returned info you can access it. Notice that adjective1, noun1 and noun2 form story function are locally scoped in that function so they are diferent from the userinput variables although they are named equally.

Sign up to request clarification or add additional context in comments.

Comments

0

Those variables are local to the function. This answer provides a good summary of the scoping in Python - Short Description of the Scoping Rules?.

You generally want to limit the available scope of variables as much as possible where practical, likely using function arguments in a case like this.

Comments

0

Try this:

def userinput():
    global  adjective1
    adjective1 = input("Adjective: ")
    global noun1
    noun1 = input("Noun: ")
    global noun2
    noun2 = input("Noun: ")

def story():
    print("A vacation is when you take a trip to some " + adjective1 + " place.")
    print("Usually you go to some place that is near " + noun1 + " or up on " + noun2 + ".")

4 Comments

Its a posibility, but teaching this to someone that dont know how to handle scope is not a very good practice :)
@DanielSanchez I find it practical and necessary when building games for example.
I've worked on a couple of titles and I think that declaring a global variable inside a function is not a good practice. Make them global by scopping them outside (and declaring global too if you require it). It's just my opinon, your code is another working option too :)
I agree with you @DanielSanchez, however that script seems to look a bit like a game and that option may be good for it.

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.