0

I am working on a function that would take an user's input for four people's names and to append them to a list. However with the code below I get this user error. However, when I declare significant_other as a global variable, I get an invalid syntax error. How would I fix my code?

Traceback (most recent call last):
  File "mash.py", line 16, in <module>
    print spouse()
  File "mash.py", line 13, in spouse
    significant_other = signficant_other.append(new_spouse)
NameError: global name 'signficant_other' is not defined

Here is the code.

import random
# import random module


def spouse():
# defined function
    significant_other =[]
    #empty list
    for x in range(4):
    # for loop for an iteration of four
        new_spouse = str(raw_input("Type a person you would like to spend your life with."))
        # new spouse input
        significant_other = signficant_other.append(new_spouse)
        #significant other gets added to the list

print spouse()

Thanks for your help with this.

0

3 Answers 3

2

You spelt significant_other like this signficant_other

plus you just need to do:

significant_other.append(new_spouse)
Sign up to request clarification or add additional context in comments.

Comments

1

change your line:

significant_other = signficant_other.append(new_spouse)

To this:

significant_other.append(new_spouse)

Basically you cannot assign a append method because it by default returns None:

>>> a = []
>>> b = a.append(5)
>>> print b
None

And so if you change your code to signficant_other.append(new_spouse) It still fails because you made a small typo you've spelled significant as signficant

Comments

0

There are a few issues, firstly:

significant_other = signficant_other.append(new_spouse)

.append() will not return a list, it modifies the list and returns None.

Secondly, you're attempting to use print, but the function that you're calling doesn't return what you want, it returns None.

Additionally, there was a spelling issue, but the above two are more important.

Try this:

def spouse():

    significant_other = []

    for x in range(4):

        new_spouse = str(raw_input("...."))
        significant_other.append(new_spouse)

    return significant_other

print spouse()

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.