[英]Making a pyramid using loops
我應該使用循環制作金字塔。 我們必須使用線和破折號來制作金字塔。 我們可以自己提出模式,但必須是線條和破折號。 我使線路部分正常工作,但是我不知道如何獲得破折號。 我的金字塔看起來不錯。
金字塔應該是什么樣的:
用戶輸入3:
-
| |
---
| | |
------
| | | |
-------
這樣的事情。 (如您所見,共有3個級別)
到目前為止,這是我的代碼。
def pyramid():
while True:
try:
height = int(input("Enter a number between 1 and 1000: "))
if 1<= height <=1000:
for i in range(0,height):
print( ' '*(height-i-1) + '|'*(2*i+1))
else:
print ("Only numbers between 1 and 1000 are allowed.")
except:
print("Numbers between 1 and 1000 only.")
pyramid()
我猜我需要2個循環,但是我應該如何開始呢?
為每個零件增加一些變量
line = 2
dash = 1
space = 2*input + 1
對於循環,請執行以下操作:
while ( dash <= ((2*input)+1) or line <= input):
print(" "*space + "- "*dash , end="")
space = space - 1
dash = dash + 2
print()
print(" "*space + "| "*line , end="")
space = space - 1
line = line + 1
print()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.