0

I have the following program

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

The output, for example if the line number is 5 and the numbers on each line is 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,

My issue is the last number of each row is followed by a commma. for example

0, 1, 2,

However my desired output is a fullstop at the end of each line. for example

0, 1, 2.

How am I able to achieve my desired output?

connorling
  • 65
  • 5
  • 1
    Can you think of a rule that tells you whether the number you're about to print is the last one on the line? (Maybe something that involves the relationship between `y` and `number_count`?) Can you think of a way to use that information so that you print the comma `if` appropriate, and the period otherwise? Hint, hint. – Karl Knechtel Mar 22 '21 at 06:20
  • You're printing with `end=', '` for every number - however, you don't want to print a comma after every number. After what numbers don't you want to print it? And when do you want to print a period? What should you change to make your code reflect that? – Grismar Mar 22 '21 at 06:23

1 Answers1

0

Add a condition?

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()
Ash W
  • 30
  • 4