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.
CodePudding user response:
range step is 1 in default.
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):
CodePudding user response:
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------------
----.|..|..|..|..|..|..|.-----
-------.|..|..|..|..|.--------
----------.|..|..|.-----------
-------------.|.--------------
