1

I'm stuck with an exercise which asks me to design a door mat of NxM size, in which N is an odd number and M is equal to N*3, in the way you can see there with the full explanation of the exercise.

To do that I've written the following code:

door_mat_length = int(input())
door_mat_width = door_mat_length * 3
string = '.|.'
welcome = 'WELCOME'

for i in range(1, int((door_mat_length + 1) / 2)):
    string_multiplier = string * (i + (i - 1))
    print(string_multiplier.center(door_mat_width, '-')) 

print(welcome.center(door_mat_width, '-'))

for i in range(int((door_mat_length + 1) / 2), 1):
    string_multiplier = string * (i + (i - 1))
    print(string_multiplier.center(door_mat_width, '-'))

But the program stops at the print command in the middle, without iterating over the next fuction. How can I resolve this? Thanks in advance.

martineau
  • 119,623
  • 25
  • 170
  • 301
  • Does it throw any exception? Or just exit with `0` exit code? – Olvin Roght Jan 08 '22 at 11:01
  • Does this answer your question? [Decreasing for loops in Python impossible?](https://stackoverflow.com/questions/4294082/decreasing-for-loops-in-python-impossible) – mkrieger1 Jan 08 '22 at 11:12

2 Answers2

1

range step is 1 in default.

Python range doc

In the document, it says For a positive step, the contents of a range r are determined by the formula r[i] = start + step*i where i >= 0 and r[i] < stop. and For a negative step, the contents of the range are still determined by the formula r[i] = start + step*i, but the constraints are i >= 0 and r[i] > stop. So your second for loop range return a empty range.

Like

for i in range(5, 1):
    print(i)

don't print anything. To solve this, you must pass a negative step to make it goes down. Like:

for i in range(5, 1, -1):
    print(i)

It prints

5
4
3
2

So if you want to use range to go down, make sure you pass a step value.

for i in range(int((door_mat_length + 1) / 2) - 1, 0, -1):
貓村幻影
  • 184
  • 6
0

change your code to this :

door_mat_length = int(input())
door_mat_width = door_mat_length * 3
string = '.|.'
welcome = 'WELCOME'

for i in range(1, int((door_mat_length + 1) / 2)):
    string_multiplier = string * (i + (i - 1))
    print(string_multiplier.center(door_mat_width, '-'))

print(welcome.center(door_mat_width, '-'))

for i in reversed(range(1,int((door_mat_length + 1) / 2) )):
    string_multiplier = string * (i + (i - 1))
    print(string_multiplier.center(door_mat_width, '-'))

input: 10

output:

-------------.|.--------------
----------.|..|..|.-----------
-------.|..|..|..|..|.--------
----.|..|..|..|..|..|..|.-----
-----------WELCOME------------
----.|..|..|..|..|..|..|.-----
-------.|..|..|..|..|.--------
----------.|..|..|.-----------
-------------.|.--------------