首页 > 解决方案 > 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.

我怎样才能达到我想要的输出?

标签: python

解决方案


添加条件?

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()

推荐阅读