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()

27th Jan 2020, 2:00 PM
Daniela Gadacz
Daniela Gadacz - avatar
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
27th Jan 2020, 3:24 PM
Ali Abdelhady
Ali Abdelhady - avatar
+ 6
okay def my_List(x) : return [x]
27th Jan 2020, 3:28 PM
Oma Falk
Oma Falk - avatar
+ 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.
27th Jan 2020, 3:57 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Ali Abdelhady your function is wrong, it would stop in the first iteration of the loop.
27th Jan 2020, 3:25 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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.
27th Jan 2020, 3:29 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 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.
27th Jan 2020, 2:09 PM
Oma Falk
Oma Falk - avatar
+ 2
Creates what list ? Your question is unclear, what do you mean by "Create list" exactly ?
27th Jan 2020, 2:21 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 1
def makeList() : l = [1,2,3] return l
27th Jan 2020, 2:28 PM
Oma Falk
Oma Falk - avatar
0
Aymane Boukrouh thanks for the reply but can you tell me why?
27th Jan 2020, 3:28 PM
Ali Abdelhady
Ali Abdelhady - avatar
0
https://code.sololearn.com/cQoO3u6eux44/?ref=app Check if this is related to your question
27th Jan 2020, 3:30 PM
Ali Abdelhady
Ali Abdelhady - avatar
0
Aymane Boukrouh so, I guess it should be print(i) instead?
27th Jan 2020, 3:55 PM
Ali Abdelhady
Ali Abdelhady - avatar