-3

I am trying to make a list in a function and print the "new" list when the user has written a country.

def main():
    while True:
        user()
        my_list()


def my_list():
    c = ["England", "Japan", "China"]
    print(c)

def user():
    country = input("Country?")
    new = my_list().append(country)
    return new

main()

But I'm getting an error with this code. I appreciate all the help.

Gringo
  • 33
  • 8
  • What kind of error – Krishna Nov 27 '18 at 16:05
  • You need to read about [variable scopes](https://stackoverflow.com/questions/291978/short-description-of-the-scoping-rules) - the list you create in one function is not know in another function unless you a) return it and store it in the other func or you provide a list to a func and it modifies it. – Patrick Artner Nov 27 '18 at 16:06
  • your my_list function has a print, but no return. when you do not have a return, you return a None instead. You should show your error message. – Paritosh Singh Nov 27 '18 at 16:06
  • `my_list()` doesn't return anything. You need to add `return c` at the end. By the way though, if that is all the function is supposed to do, you could just write `my_list = ["England", "Japan", "China"]` as a variable somewhere, you don't need a function for it – Karl Nov 27 '18 at 16:06
  • @Karl Thank u. I just wrote my_list = ["England", "Japan", "China"] and it solved my problem. – Gringo Nov 27 '18 at 16:12

1 Answers1

0

try this:

def main():
    c = []
    while True:
        country = input("Country?")
        c.append(country)
        for i in c:
            print(i)
main()
Louis
  • 1