0

Nearest Bathroom Practise (From Lists topic) - Test Case Error

I tried to code using lists and for loops to find the nearest bathroom. But one of the test case validation failed. I couldn't spot the error. Kindly help me on this. Below is the description & code. Thanks. Description: A group of buildings have restrooms on every 5th floor. For example, 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 Code: n = int(input()) if n%5 == 0: x = list(range(5,n,5)) for y in x: print(y) elif n%5 == 1: x = list(range(5,n,5)) for y in x: print(y) elif n%5 == 2: x = list(range(5,n-1,5)) for y in x: print(y) elif n%5 == 3: x = list(range(5,n+3,5)) for y in x: print(y) else: x = list(range(5,n+4,5)) for y in x: print(y)

7th Sep 2021, 12:59 PM
Jowin Isaac
Jowin Isaac - avatar
4 ответов
0
Please post the task description.
7th Sep 2021, 1:01 PM
Simon Sauter
Simon Sauter - avatar
+ 1
Your code is far more complicated than necessary. Just loop through the numbers 1 through n and print the numbers that are divisible by 5 without remainder.
7th Sep 2021, 1:31 PM
Simon Sauter
Simon Sauter - avatar
0
Alternatively you can loop through the multiples of five and print the ones that are smaller or equal to n. You can do that by using a while loop.
7th Sep 2021, 1:37 PM
Simon Sauter
Simon Sauter - avatar
0
Hi Jowin! Your code gives wrong output if input ends with 4 or 9. I mean it fails in else part. But you can achieve this practice using single for loop. For that, you can use if statement inside loop to check whether input is divisible by 5 or not.
7th Sep 2021, 5:26 PM
Python Learner
Python Learner - avatar