所以我正在玩一个标签树。
#Ask user to enter number of tree rows
Height=int(input("Enter height of tree: "))
hashes = 1
while Height >0:
print(' ' * (Height-1) + "#" * (hashes))
Height -=1
hashes +=2
#print a stump(hash)
print(" " * (Height) + "#")
对于最后一次打印,Height变量中指定的值似乎为0.它保持值在while循环中递减。如何在while循环回到初始值后重置它?
答案 0 :(得分:0)
在循环之前保存其值:
Height=int(input("Enter height of tree: "))
hashes = 1
h = Height
while Height >0:
print(' ' * (Height-1) + "#" * (hashes))
Height -=1
hashes +=2
#print a stump(hash)
print(" " * (h-1) + "#")
答案 1 :(得分:0)
不使用while
循环,而是使用单独的循环变量循环range(Height-1, -1, -1)
height = int(input("Enter height of tree: "))
for hashes, curr_height in enumerate(range(height-1, -1, -1)):
print((' ' * curr_height) + ('#' * (2*hashes + 1)))
print(' ' * (height-1) + '#')