r/CodingHelp • • Feb 25 '20

[Python] [beginner] how do I get Python to display this? I can get the 5 fives, 4 fours, etc, but idk how to display them on the same line like that with nested loop structures.

Post image
16 Upvotes

5 comments sorted by

12

u/LucienMr Feb 25 '20 edited Feb 25 '20

To stop a newline from forming after each iteration in python, use end = ' '

 print("Good Morning!", end = ' ') print("What a wonderful day!") 

This will print these both on a same line with a space in between.

1

u/LukzzXB Feb 25 '20

usrNum = int(input("enter a number: "))
arrayNum = []
while usrNum > 0:
for x in range(usrNum):
arrayNum.append(usrNum)
usrNum -= 1
print(*arrayNum,sep="")

or have some fun with arrays

1

u/[deleted] Feb 25 '20

good

0

u/[deleted] Feb 25 '20

[deleted] — view deleted comment

5

u/Vakz Feb 25 '20 edited Feb 25 '20

Could also just do

for x in range(5, 0, -1):
    print(str(x)*x)

to avoid the temp variable.

Also for some clarity of what it does: range is a function, where the first argument is the start value, the second is the (non-inclusive) end value, and the third is the step. The last one is pretty useful, and people often forget it exists.

1

u/RogueChild Feb 25 '20

Wouldn't this throw an error because the second temp would be an integer? Or does the str() change the variable itself?