Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

With a while loop, how it is possible to output a number list with an ending dot for each number?

based on that code, I wish to output my result by ending each digit with a dot.

input:

x = 0
while x<=5:
  print(x)
  x += 1

expected output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1.
2.
3.
4.
5.

>Solution :

Make sure you start at x = 1 and add a full stop to the string. Concatenating more letters or symbols to variables can be done with f-strings:

# Start at 1
x = 1
while x <= 5:
    print(f'{x}.')
    x += 1

Alternatively, you could turn this into a one liner function:

>>> def ordered(start, stop):
...     return '\n'.join(f'{x}.' for x in range(start, stop+1))
... 
>>> print(ordered(1, 5))
1.
2.
3.
4.
5.
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading