0

Error in python practice solution?

My opinion, Nearest Restaurant practice in Introduction to Python the correct answear is for i in range(1, floors+1): But the solution says for i in range(1, floors): there is no output if the input is 5. This for loop never go to the last floor.

28th Jan 2023, 4:27 AM
Kukusik Péter
4 Answers
0
You are correct that the for loop in the official solution is incorrect, as it will not iterate to the last floor due to the "left inclusive interval" nature of range(). 0 <= x <= 1 = ....(start, end] The correct solution should be to use the range function with the argument range(1, floors+1) instead of range(1, floors) in order to include the last floor in the iteration. This will ensure that all floors that are multiples of 5 are included in the output.
28th Jan 2023, 12:46 PM
Mirielle
Mirielle - avatar
+ 7
Kukusik Péter , the issue is not the for loop. > when using a range like: *range(3, 10)* the start number (3 ) is included in the range. the stop number (10) is excluded, so this range generates: 3, 4, 5, 6, 7, 8, 9 > for our case to get the stop number included we have to use: range(3, 10 +1)
28th Jan 2023, 12:14 PM
Lothar
Lothar - avatar
+ 1
Kukusik Péter Share the link of the problem
28th Jan 2023, 5:20 AM
R🌸🇮🇳
R🌸🇮🇳 - avatar
0
The challenge: Nearest Restaurant A group of buildings has a restaurant on every 5th floor. In other words, a building with 12 floors has restaurants on the 5th and 10th floors. Task Create a program that takes the total number of floors as input and outputs the floors that have restrooms. Sample Input 17 Sample Output 5 10 15 Official solution: floors = int(input()) for i in range(1, floors): if i%5 == 0: print(i) I think this for loop wrong because never go to the last floor. The correct is for i in range(1, floors+1):
28th Jan 2023, 11:17 AM
Kukusik Péter