0
Writing a function that creates a list and prints elements
I tried creating a list and for loop to print elements. #python I was told that the function below, doesn’t create the list, it is using a hard-coded list instead. How can I fix it? def list_iterate(): list1 = ["I", "Like", "Eating", "Banana", "Bread"] for element in list1: print(element) list_iterate()
11 Antworten
0
Let's say you have an input stored in a variable (variable "a" for example)
You want the function to create a list, then the function shall have one argument
def my_List(x):
x = list(x)
for i in x:
return i
print(my_List(a))
I hope I could help
+ 6
okay
def my_List(x) :
return [x]
+ 5
Ali Abdelhady print(i) won't return anything either, it will just print.
This whole post is unclear, we're still waiting for the original poster to answer.
+ 4
Ali Abdelhady your function is wrong, it would stop in the first iteration of the loop.
+ 3
Ali Abdelhady because the return statement will immediatly stop the function. In this case, the function will only return the first element of the list.
+ 2
hey... seems u put already some brain in it.
Please hack the code in SL ib coding section and post the link.
Doesnt matter if it is not running.
+ 2
Creates what list ? Your question is unclear, what do you mean by "Create list" exactly ?
+ 1
def makeList() :
l = [1,2,3]
return l
0
Aymane Boukrouh thanks for the reply but can you tell me why?
0
https://code.sololearn.com/cQoO3u6eux44/?ref=app
Check if this is related to your question
0
Aymane Boukrouh so, I guess it should be print(i) instead?