0

How to put elements from a list into new lines, whappened with a list, what exactly does "for...in....range" do, wrong solution

"""A group of buildings have restrooms every 5th floor. In other words, a building with 12 floors has restrooms on the 5th and 10th floors. Create a program that takes the total number of floors as input and outputs the floors that have restrooms. Sample Input 23 Sample Output 5 10 15 20 """ n = int(input())+1 bathrooms=list(range(5,n,5)) #There are 3 examples, first one print(bathrooms) #gives the result like "[5,10,15]" which is ok I guess #then this one for bathrooms in range(5,n,5): print(bathrooms) #gives the desired result which is also ok I guess, although I haven't quite comprehended this lesson (for .. in ... range) yet. #but if I repeat print(bathrooms) #now the result is "15". Can somebody explain whappened here? When and how was this redefined? Why this used to be a [list], now it's not? #also how would I put the result from the first example into new lines? I googled sep = '\n' but I'm still lost, not sure how to use it. #also I think the solution for this exercise on Sololea

4th Nov 2021, 10:47 PM
Jona Žonta
Jona Žonta - avatar
6 Respostas
+ 2
Jona Žonta Your second example gets a bit confusing because you are using your list variable as the placeholder for the range loop, which means that when you print(bathroom), is the code going to print the placeholder, or the list? Also, if you have a list which you wish to iterate through, then you don't need a range() for num in brm: print(num) RESULT 5 10 15 20 Because your print function keeps getting called by the loop, it outputs each item on a new line. You don't need sep="\n"
5th Nov 2021, 12:51 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Hi Jona Žonta Your question is long, complicated and involves a number of subjects, so I will try to address a few in separate posts. FOR LOOP for i in range(1,10): Lets break it down into pieces A range of numbers can be specified by a Start number and an End number, so range (1,10) will contain 1,2,3,4,5,6,7,8,9 "i" is used as a variable placeholder used to contain the value of the iteration. Another way of writing a for loop -> for each_item in range(1,10): Which could be written in English as -> for each item in the range of numbers from 1 to 10 The term i is used as a shortcut to reduce excessive typing, but you may place any meaningful term you wish instead. for num in range(10) for item in range(10) for girlfriend in range(10) etc.....
5th Nov 2021, 12:15 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Jona Žonta Let's look at your first example brm = list(range(5, 22, 5)): Your range creates a series of numbers from 5 to 21, then slices every 5th number to present as the included numbers -> 5,10,15,20 Now brm = list(5,10,15,20) You want that range turned into a list of items that may be manipulated /used. brm = [5, 10, 15, 20]
5th Nov 2021, 12:29 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Sololearn accepts wrong submissions because it doesn't actually check your code. It just checks whether your code produces the expected output for a limited number of inputs.
5th Nov 2021, 1:20 AM
Simon Sauter
Simon Sauter - avatar
0
/.../Sololearn is wrong because it doesn't care whether I put n+1 or not, but in fact if I hadn't there would be no bathroom observed on the last floor if the last floor was a multiple of 5 since maximum isn't included.
4th Nov 2021, 10:47 PM
Jona Žonta
Jona Žonta - avatar
0
Rik Wittkopp My initial attempt was for n in bathrooms: print(bathrooms) It didn't work since it should be print(n), but I didn't think of that, instead I got discouraged, changed everything and everything got more confusing. Thank you so much, it's clearer now.
5th Nov 2021, 7:26 AM
Jona Žonta
Jona Žonta - avatar