[英]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.