+ 1

Confused about this.

can someone explain to me why print("1.\n2.\n3.\n4.\n5.\n6.\n7.\n8.\n9.") is correct and print ("""1. 2. 3. 4. 5. 6. 7. 8. 9.""") is wrong. The problem was to output the numbers 1 to 9, each on a separate line, followed by a dot. both of the codes do the same thing, but the website accepts the code with /n.

31st Oct 2021, 3:17 AM
Orpheus Lumen
Orpheus Lumen - avatar
3 Answers
+ 5
Orpheus Lumen , when removing all trailing whitespace from the second code sample it will run cuccessfully for code coach.
31st Oct 2021, 8:18 AM
Lothar
Lothar - avatar
+ 4
Both of your codes do the same thing, the result is exactly the same. Maybe the website you mention, validates also the code itself. But generally in this situation it would be best to use a loop! Always think about that when you have to code a repeating operation (in this case: print a number). for x in range(1, 10): print(str(x) + '.')
31st Oct 2021, 4:19 AM
Tibor Santa
Tibor Santa - avatar
+ 2
Python for Beginners, 13 – maybe you have some white space character in the 2nd example?
31st Oct 2021, 4:52 AM
Lisa
Lisa - avatar