我堅持與要求我設計的門墊鍛煉NxM的大小,其中N的奇數和M是等于N*3,在路上就可以看到有行權的充分解釋。
為此,我撰寫了以下代碼:
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, '-'))
但是程式在中間的 print 命令處停止,沒有迭代下一個函式。我該如何解決這個問題?提前致謝。
uj5u.com熱心網友回復:
范圍步長默認為 1。
Python 范圍檔案
在檔案中,它說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.,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.所以你的第二個 for 回圈范圍回傳一個空范圍。
喜歡
for i in range(5, 1):
print(i)
不要列印任何東西。要解決這個問題,您必須通過一個負面步驟以使其下降。喜歡:
for i in range(5, 1, -1):
print(i)
它列印
5
4
3
2
因此,如果您想使用范圍向下移動,請確保您傳遞了一個步長值。
for i in range(int((door_mat_length 1) / 2) - 1, 0, -1):
uj5u.com熱心網友回復:
將您的代碼更改為:
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, '-'))
輸入:10
輸出:
-------------.|.--------------
----------.|..|..|.-----------
-------.|..|..|..|..|.--------
----.|..|..|..|..|..|..|.-----
-----------WELCOME------------
----.|..|..|..|..|..|..|.-----
-------.|..|..|..|..|.--------
----------.|..|..|.-----------
-------------.|.--------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/405942.html
標籤:
