Solved
My exercise is to write a function called list_check, which accepts a list from the user and returns True if every element in the userlist is itself also a list.
The bottom line is I would like to see a working example of this problem solved using user input, which is more difficult than just supplying the list yourself.
This is the closest I’ve come accepting the user’s input for the list:
userlist = []
number_of_elements = int(input("Enter the number of elements in your list: "))
for i in range(0, number_of_elements):
element = input().split()
userlist.append(element)
if all(isinstance(element, list) for element in userlist):
print("True")
else:
print("False")
The working code without user input is as follows:
customlist = [[1,2,3],[2,3,4], False]
def list_check(customlist):
answer = all(type(l) == list for l in customlist)
print(answer)
list_check(customlist)
False
Appreciate the help. - J
def listcheck():
y = (input("Enter your lists: \n"))
if y[0] !="[" or y[1] !="[":
print("false, you entered data not starting with [[")
return False
if y[len(y)-1] !="]" or y[len(y)-2] !="]":
print("false, you entered data not ending with ]]")
return False
import ast
z = ast.literal_eval(y)
def innerlistcheck(alist):
for x in range(0, len(alist), 1):
if type(alist[x]) != list:
print("false, " + str(alist[x]) + " is not a list")
return False
print("true")
return True
innerlistcheck(z)
listcheck()
I think this may be an answer to your question. The hardest part was seeing how to convert a string to a list which I stole from here: Convert string representation of list to list