Python Forum
function 'return' and file content into a list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
function 'return' and file content into a list
#1
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)
Reply
#2
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
Reply
#3
(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
Reply
#4
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.
Reply
#5
(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
Test everything in a Python shell (iPython, Azure Notebook, etc.)
  • Someone gave you an advice you liked? Test it - maybe the advice was actually bad.
  • Someone gave you an advice you think is bad? Test it before arguing - maybe it was good.
  • You posted a claim that something you did not test works? Be prepared to eat your hat.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 15,679 Jan-05-2024, 08:30 PM
Last Post: sgrey
  [solved] list content check paul18fr 6 704 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  nested function return MHGhonaim 2 613 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,283 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,183 Feb-04-2023, 12:30 PM
Last Post: caslor
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,996 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Need to parse a list of boolean columns inside a list and return true values Python84 4 2,108 Jan-09-2022, 02:39 AM
Last Post: Python84
  return vs. print in nested function example Mark17 4 1,744 Jan-04-2022, 06:02 PM
Last Post: jefsummers
Thumbs Up Parsing a YAML file without changing the string content..?, Flask - solved. SpongeB0B 2 2,275 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B
  How to invoke a function with return statement in list comprehension? maiya 4 2,843 Jul-17-2021, 04:30 PM
Last Post: maiya

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020