python - Python如何打印替代的“结束”语句
问题描述
我有以下程序
#ask user number of lines to display
user_input = input("How many lines to display? ")
line_count = int(user_input)
#ask users how many numbers on each line
user_input = input("How many numbers on each line? ")
number_count = int(user_input)
i=0
for x in range(line_count ):
for y in range(number_count):
print(i, end=', ')
i+=1
print()
输出,例如,如果行号为 5,每行上的数字为 3。
How many lines to display? 5
How many numbers on each line? 3
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11,
12, 13, 14,
我的问题是每行的最后一个数字后跟一个逗号。例如
0, 1, 2,
但是,我想要的输出是每行末尾的句号。例如
0, 1, 2.
我怎样才能达到我想要的输出?
解决方案
添加条件?
for x in range(line_count):
for y in range(number_count):
if y < number_count-1:
print(i, end=', ')
else:
print(i, end='. ')
i+=1
print()
推荐阅读
- asp.net - 如何在Angular中返回布尔值
- pycharm - pycharm 不显示代码建议或颜色 python 代码
- css - React 中表格行的样式
- linux - 如何使用 grep 和正则表达式仅显示 3 个字符的名称
- javascript - 使用 Firebase 部署 Nuxt 站点地图
- sql - 如果它们以数字命名,如何访问列
- macos - Mac 64 位中的多处理运行时错误 freeze_support()
- tensorflow - 在 Keras 中向 BiLSTM 模型添加 CRF 层(2021 年 1 月)
- aws-lambda - 用存根客户端覆盖 boto3 客户端
- asp.net - 添加带有单词扩展名的脚注