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
#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


Messages In This Thread
RE: function 'return' and file content into a list - by volcano63 - Jul-06-2018, 10:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  what to return for an empty list Skaperen 2 299 May-24-2024, 05:17 PM
Last Post: Skaperen
  How do I calculate a ratio from 2 numbers and return an equivalent list of about 1000 Pleiades 8 16,141 Jan-05-2024, 08:30 PM
Last Post: sgrey
  [solved] list content check paul18fr 6 910 Jan-04-2024, 11:32 AM
Last Post: deanhystad
  nested function return MHGhonaim 2 754 Oct-02-2023, 09:21 AM
Last Post: deanhystad
  return next item each time a function is executed User3000 19 2,669 Aug-06-2023, 02:29 PM
Last Post: deanhystad
  function return boolean based on GPIO pin reading caslor 2 1,332 Feb-04-2023, 12:30 PM
Last Post: caslor
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,254 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,267 Jan-09-2022, 02:39 AM
Last Post: Python84
  return vs. print in nested function example Mark17 4 1,891 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,383 Aug-05-2021, 08:02 AM
Last Post: SpongeB0B

Forum Jump:

User Panel Messages

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