I want to make a Python program that prints an amount of *'s based on a list. So if a list was numlist = [2,5,3,6]
it would output:
**
*****
***
******
I also want to use a function to do this, here is my code:
def histogram(numlist):
for num in numlist:
print()
numlist = [2,5,3,6]
histogram(numlist)
I left the print blank because that is the part I am having trouble on.
This is the most interesting part
What did you think about trying and how did that turn out?
Well I tried print('*'[numlist]
but it gave the error
File "/Users/eric/Desktop/histogramEric.py", line 12, in <module>
histogram(numlist)
File "/Users/eric/Desktop/histogramEric.py", line 9, in histogram
print("*"[num])
IndexError: string index out of range
Think about how ‘*’ and num could form the output that you want.
Did you know that you can multiply a string by a number?
@albionsrefugeI [quote=“albionsrefuge, post:4, topic:22389”]
Did you know that you can multiply a string by a number?
[/quote]
Was thinking about that but didn’t know that it was possible.
1 Like