number_iterations = input('enter how many times: ')
list_2 = []
list_3 = []
list_4 = []
list_5 = []
list_6 = []
list_7 = []
list_8 = []
list_9 = []
list_10 = []
list_11 = []
list_12 = []
n = int(number_iterations)
for i in range (1, n):
x = random.randint(1,6)
y = random.randint(1,6)
sum_num = x y
if sum_num == 2:
list_2.append(1)
elif sum_num == 3:
list_3.append(1)
elif sum_num == 4:
list_4.append(1)
elif sum_num == 5:
list_5.append(1)
elif sum_num == 6:
list_6.append(1)
elif sum_num == 7:
list_7.append(1)
elif sum_num == 8:
list_8.append(1)
elif sum_num == 9:
list_9.append(1)
elif sum_num == 10:
list_10.append(1)
elif sum_num == 11:
list_11.append(1)
elif sum_num == 12:
list_12.append(1)
two = '* ' * (len(list_2))
three = '* ' * (len(list_3))
four = '* ' * (len(list_4))
five = '* ' * (len(list_5))
six = '* ' * (len(list_6))
seven = '* ' * (len(list_7))
eight = '* ' * (len(list_8))
nine = '* ' * (len(list_9))
ten = '* ' * (len(list_10))
eleven = '* ' * (len(list_11))
twelve = '* ' * (len(list_12))
print('2 : ' two)
print('3 : ' three)
print('4 : ' four)
print('5 : ' five)
print('6 : ' six)
print('7 : ' seven)
print('8 : ' eight)
print('9 : ' nine)
print('10: ' ten)
print('11: ' eleven)
print('12: ' twelve)
Basically what the code does is simulate the rolling of two dice a user defined number of times. The goal is to graph these results. An example output would look like:
enter how many times: 100
2 : * * *
3 : * * * * * *
4 : * * * * * * *
5 : * * * * * * * * * * * * * *
6 : * * * * * * * * * * * * * * * * *
7 : * * * * * * * * * *
8 : * * * * * * * * * * * * *
9 : * * * * * * * * * * * * * * *
10: * * * * * *
11: * * * * * *
12: * *
This of course is a horizontal bar graph. I was wondering if there was a way to make it so that it was a vertical one, with the bars displaying vertically? I've tried using itertools and .zip_longest but it doesn't work at all. Any help would be appreciated, thanks !
CodePudding user response:
You could try to first find the maximum number of asterisks. Then, iterate row in range(max_asterisks, 0, -1)
.
For each row, you go over your lists, and print an asterisk if the count exceeds the row number, or print a space if not.
For example, this is what I came up with:
# counts is the number of elements in each of your lists
# print(*counts, sep='\t')
for lineno in range(max(counts), 0, -1):
# 2-12: eleven elements
for item in range(11):
# Print only if the count in this column is large enough
if counts[item] >= lineno:
print('*', end='')
# TABs just make formatting easier
print('\t', end='')
# Add newline
print()
# Add the column label
print(*range(2, 13), sep='\t')
And the result:
*
*
* *
* * *
* * * * *
* * * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
2 3 4 5 6 7 8 9 10 11 12