Jul-06-2018, 12:58 AM
Hi
I have again an issue with an error and it seems that it is due to my difficulties to understanding how to properly use the "return" when defining a function.
In the Microsoft Learning portal, there is this final test that requires to create the function to get a list of names and then call it later.
so I wrote the code below:
However, the error 'userlist is not defined' comes up.
Can somebody explain me simply how I can create a list from user input within a function outside of it?
In other words, if I create a list in a function, how can I use it later in the code?
Also, is there a cleaner way to write what I wrote?
Thank you veery much in advance for your kind help.
Shiro
I have again an issue with an error and it seems that it is due to my difficulties to understanding how to properly use the "return" when defining a function.
In the Microsoft Learning portal, there is this final test that requires to create the function to get a list of names and then call it later.
so I wrote the code below:
However, the error 'userlist is not defined' comes up.
Can somebody explain me simply how I can create a list from user input within a function outside of it?
In other words, if I create a list in a function, how can I use it later in the code?
Also, is there a cleaner way to write what I wrote?
Thank you veery much in advance for your kind help.
Shiro
### GET THE 3 USER ANSWERS #============================== def get_names(): userlist = [] #setup to contain only lower case data print("List any 3 of the first 20 elements in the Period table.") while len(userlist)<3: userinput = input("Enter the name of an element:\t") if userinput.isalpha: if userlist.count(userinput) == 0: userlist.append(userinput.lower()) #print(userinput.title() + "was already entered, no duplicates allowed.") else: print(userinput.title() + "was already entered, no duplicates allowed.") #userlist.append(userinput.lower()) else: pass return userlist ### 20 ELEMENTS ANSWERS IMPORT #============================= !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/elements1_20.txt -o elements1_20.txt E_20_file = open('elements1_20.txt','r') print() ###READING 20 ELEMENTS ANSWERS #============================= #IMPORT ATTEMPT2 BEG #-------------------- E_20_L1 = E_20_file.readline().strip() ELIST = [] while E_20_L1: ELIST.append(E_20_L1.lower()) E_20_L1 = E_20_file.readline().strip() print("E_20_L1:\n",ELIST,"\n\n") #------------------- #IMPORT ATTEMPT2 END elements1_20_file.close() get_names() correctans = 0 correctanslist = [] incorrectans = 0 incorrectanslist = [] for inputnames in userlist: if ELIST.index(inputnames) != 0: correctans += 1 correctanslist.append(inputnames) else: incorrectans += 1 incorrectanslist.append(inputnames) #Calculate the % correct correctpercent = correctans*20 #report print print("Score %:\t",correctpercent) print("Your correct answer list:\n",correctanslist) print("Your incorrect answer list:\n",incorrectanslist)