any alternatives? | Sololearn: Learn to code for FREE!

+4

any alternatives?

So the question was "You need to make a program for a leaderboard. The program needs to output the numbers 1 to 9, each on a separate line, followed by a dot: 1. 2. 3. ..." and my answer was: l = [1,2,3,4,5,6,7,8,9] for lis in l: print(str(lis) + ".") I know that is a for loop and I felt like cheating because for loop is the only one came up on my mind. any alternatives?

10/17/2021 7:12:02 PM

Naung Mon Oo

7 Answers

New Answer

+4

w/o cheating 😉 print(""" 1. 2. 3. 4. 5. 6. 7. 8. 9.""")

+5

for i in range(1,10): print(str(i)+".")

+3

While loop could also be used. myList = [1, 2, 3, 4, 5, 6, 7, 8, 9] i=0 while i<len(myList): print(str(myList[i])+".") i+=1

+3

for i in range(9): print(i+1,end=".\n")

+2

print (*list(str(x) + '.' for x in range(1, 10)),sep = "\n") it is one line.

+1

try this: def lb(x): if x < 10: print(str(x)+'.') x += 1 return lb(x) lb(1)

+1

How about this? from string import digits print('.\n'.join(list(digits[1:])+['']))