[英]Returns wrong output for the pyramid problem
對於這個編碼練習,我必須輸入一些虛構的塊,它會告訴我金字塔有多少完整的行高。
例如,如果我輸入 6 個塊,我希望它告訴我金字塔的高度是 3(底部 3 個塊,上方 2 個塊,上方 1 個塊)。
blocks = int(input("Enter the number of blocks: "))
height=0
count=1
while(blocks>1):
for i in range(0,count):
blocks -= 1
count +=1
height += 1
print("The height of the pyramid:", height)
它適用於 6,但對於 1000,它應該返回 44 但我得到 45? 我的代碼有什么問題?
您需要在 for 循環之前添加計數並添加 =。
blocks = int(input("Enter the number of blocks: "))
height=0
count=1
while(blocks>=1):
count +=1
height += 1
for i in range(0,count):
blocks -= 1
print("The height of the pyramid:", height)
這是一個只有一個while循環的實現
number=1000
count=0
while number >= count:
count +=1
number -= count
print(count)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.