python - 有问题要理解金字塔中的while循环
问题描述
谁能解释如何在 python 中使用 while 循环创建金字塔?
userInput = int(input("Please enter the amount of rows: "))
row = 0
while(row < userInput):
row += 1
spaces = userInput - row
spaces_counter = 0
while(spaces_counter < spaces):
print(" ", end='')
spaces_counter += 1
num_stars = 2*row-1
while(num_stars > 0):
print("*", end='')
num_stars -= 1
print()
如果有人向我解释该代码,那就太好了..thx
解决方案
你只需要修复你的缩进:
userInput = int(input("Please enter the amount of rows: "))
row = 0
while(row < userInput):
row += 1
spaces = userInput - row
spaces_counter = 0
while(spaces_counter < spaces):
print(" ", end='')
spaces_counter += 1
num_stars = 2*row-1
while(num_stars > 0):
print("*", end='')
num_stars -= 1
print() # next line
输出
Please enter the amount of rows: 5
*
***
*****
*******
*********
推荐阅读
- swift - 全屏模式下另一个应用程序前面的“NSApp.activate”
- tensorflow - 在训练期间无法更改 tensorflow 优化器中的 learning_rate
- amazon-web-services - 将 ElasticSearch 添加为 AWS Data Migration Service 的目标时连接失败
- javascript - Mongoose中的日期范围查询将循环结构转换为JSON
- r - 如何更改ggplot2中的轴线大小?
- java - Java中的中位数和平方根
- python - 如何使用循环将列表附加到字典
- php - 是否可以使用 Migrating 和 Seed Once 进行 InMemory 测试
- python - 代码不能作为函数工作,但可以在主代码中工作。(Python,pygame)
- django - Ajax - LocalHost 上的 500 内部服务器错误