Python Forum

Full Version: function 'return' and file content into a list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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


### 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)
for one, when you call get_names, you need to assign the return value to a variable something like:
users = get_names()
Users will be a list with same contents as that created in function
(Jul-06-2018, 01:17 AM)Larz60+ Wrote: [ -> ]for one, when you call get_names, you need to assign the return value to a variable something like:
users = get_names()
Users will be a list with same contents as that created in function

Hi
Thank you very much for your answer.

Does that mean that I can remove the line "return userlist" in the function definition?

Thank you,
Shiro
if you don't return any values, why run the function at all?
why would you want to delete the return?
If you don't need the returned value, ignore it.
(Jul-06-2018, 12:58 AM)shiro26 Wrote: [ -> ]
### 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: <----- 1
			if userlist.count(userinput) == 0: <----- 2
				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: <----- 3
			pass
	return userlist
.....

  1. You are not calling a function - you are checking that userinput.isalpha is non-empty object (it is). Add brackets.
  2. You add lowercase string - but you check string as is. Lower-case the string before the test - and don't use count, use in operator
    if userlist in userinput
  3. Redundant
General comment - separate words in variable names with underscores, see PEP-8