r/CodingHelp • u/[deleted] • 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.
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 -= 1print(*arrayNum,sep="")
or have some fun with arrays
1
0
Feb 25 '20
[deleted]
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
startvalue, the second is the (non-inclusive)endvalue, 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?
12
u/LucienMr Feb 25 '20 edited Feb 25 '20
To stop a newline from forming after each iteration in python, use
end = ' 'This will print these both on a same line with a space in between.