today I bring you an apparently simple question, that it's not so simple as it seems(for me at least)!
Imagine I have the following list of integers:
num = [3,1,1,2]
And I want to print "$" corresponding with the height i.e:
&
& &
& & & &
for i in num:
print("#"*i)
prints this:
& & &
&
&
& &
However I want the former displayed!
I tried this:
for i in range(1, max(num)+1): # loops through the rows
for j in num:
if j == i:
print("#")
else:
print("")
But after a while I understood that the condition doesn't make any sense, because I'm comparing row numbers with the height!
I tried other stuff but none of them worked properly, I would appreciate if someone could help me out! Thanks
I would just iterate backwards from the max number, checking each element in your list if it is greater to or equal to that number, and printing the desired character, else printing a space.
>>> for x in range(max(num), 0, -1):
... print(''.join(['&' if i >= x else ' ' for i in num]))
...
&
& &
&&&&
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With