Reputation: 7
I have a problem writing a program with for loops. I am not sure how to go about it but I first of all looped through a string containing a number. And now I need to replicate a string '#' with the individual numbers within that number e.g. I want to replicate the string '#' by replicating it with 2 from '274878'. This is my code so far:
bars_string = input('Enter bars string:\n ')
print('+---------+')
for element in bars_string:
pass
This is my expected output for a given number string:
Enter bars string:
2378945
+---------+
|## |
|### |
|####### |
|######## |
|#########|
|#### |
|##### |
+---------+
Upvotes: 1
Views: 189
Reputation: 42143
You can use format strings:
barString = "2378945"
print("+---------+")
print(*(f"|{'#'*int(n):9}|" for n in barString),sep="\n")
print("+---------+")
+---------+
|## |
|### |
|####### |
|######## |
|#########|
|#### |
|##### |
+---------+
Upvotes: 1
Reputation: 4761
You can do it this way:
bars_string = input('Enter bars string:\n ')
digits = list(map(int, bars_string))
max_digit = max(digits)
print("+" + "-"*max_digit + "+")
for digit in digits:
print("|" + "#"*digit + " "*(max_digit - digit) + "|")
print("+" + "-"*max_digit + "+")
Upvotes: 2
Reputation: 79
To do this, you need to times the #
with the element
in integer form. And in python, you can literally times strings with a number! Here's the code:
bars_string = input('Enter bars string:\n ')
#2378945
for element in bars_string:
print('#'*int(element))
Upvotes: 0