any alternatives? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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?

17th Oct 2021, 7:12 PM
Naung Mon Oo
Naung Mon Oo - avatar
6 Answers
+ 4
w/o cheating 😉 print(""" 1. 2. 3. 4. 5. 6. 7. 8. 9.""")
17th Oct 2021, 7:23 PM
Coding Cat
Coding Cat - avatar
+ 5
for i in range(1,10): print(str(i)+".")
17th Oct 2021, 7:17 PM
Coding Cat
Coding Cat - avatar
+ 3
for i in range(9): print(i+1,end=".\n")
17th Oct 2021, 7:39 PM
Jayakrishna 🇮🇳
+ 2
print (*list(str(x) + '.' for x in range(1, 10)),sep = "\n") it is one line.
17th Oct 2021, 7:37 PM
Paul
Paul - avatar
+ 1
try this: def lb(x): if x < 10: print(str(x)+'.') x += 1 return lb(x) lb(1)
17th Oct 2021, 7:51 PM
ubai
ubai - avatar
+ 1
How about this? from string import digits print('.\n'.join(list(digits[1:])+['']))
20th Oct 2021, 12:32 PM
Steve
Steve - avatar