Home > Blockchain >  How do I different values of stars vertically instead of horizontally?
How do I different values of stars vertically instead of horizontally?

Time:10-06

I have a program that "rolls" 2 dice 100 times and adds the values of each roll to a list. I then count the number of times 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, and 12 appear in that list, and that is how many times the combination appeared in the rolls. Right now, I am displaying my data by concatenating a string with the number, the number of times it appears, and then a newline for the next number.(see the image) https://i.stack.imgur.com/qxUGr.png However, I wish to display my "graph" vertically, like the second image attached: https://i.stack.imgur.com/5KvzU.png

I've tried multiple things such as putting everything including the stars in one mega list and then zipping them, however that was unsuccessful. I also tried to write a loop that prints stars in rows however I can't seem to make sense of it.

def q6():
c = 0
c1 = 0
results = []
for x in range(100):
    c = random.randint(1, 6)
    c1 = random.randint(1, 6)
    sum = c   c1
    results.append(sum)
num2 = results.count(2)
num3 = results.count(3)
num4 = results.count(4)
num5 = results.count(5)
num6 = results.count(6)
num7 = results.count(7)
num8 = results.count(8)
num9 = results.count(9)
num10 = results.count(10)
num11 = results.count(11)
num12 = results.count(12)
display = "2: "   (num2 * "*")   "\n"   "3: "   (num3 * "*")   "\n"  "4: "   (num4 * "*")   "\n"  "5: "   (num5 * "*")   "\n"  "6: "   (num6 * "*")   "\n"  "7: "   (num7 * "*")   "\n"  "8: "   (num8 * "*")   "\n"  "9: "   (num9 * "*")   "\n"  "10: "   (num10 * "*")   "\n"  "11: "   (num11 * "*")   "\n"  "12: "   (num12 * "*") 
return display
#below is a loop I tried to do by printing the stars in a row and printing a space if the value was 0. the variable called "big" is the largest number in the list of values, variable "layers" was the layer the loop was printing at the time. "string" is of course the display.
"""while layers < big:
    for x in counts[x]:
        y = 0
        if x > 0:
            string  = " * "
            counts[y] = counts[y] - 1
        if x == 0:
            string  = "  "
        y  = 1
    string  = "\n" 
    layers  = 1
    
return string"""

This is my code so far.

CodePudding user response:

Try the following:

import random

def q6():
    d = {k: 0 for k in range(2, 13)}
    for _ in range(100):
        d[random.randint(1, 6)   random.randint(1, 6)]  = 1 # random draw

    output = ' '.join(f'{k:2}' for k in d) # header

    while any(d.values()): # while there are any remaining stars to be drawn
        line = '' # begin with an empty string
        line = ' '.join(' *' if v else '  ' for v in d.values())
        for k in d: # loop over d to reduce the number of remaining stars
            if d[k] > 0:
                d[k] -= 1
        output  = '\n'   line # append the line to the output

    return output

print(q6())

As you can note, the idea is to reduce the number of stars by 1 at each step, until the numbers are depleted.

Note that the number of stars, i.e., the dict d, will not be kept intact. In case you want to keep that, use something like d_copy = d.copy() before the while loop.

Also this approach uses the fact that dict preserves the order of the items (based on insertion) since python 3.7.

Output:

 2  3  4  5  6  7  8  9 10 11 12
 *  *  *  *  *  *  *  *  *  *  *
 *  *  *  *  *  *  *  *  *  *  *
 *  *  *  *  *  *  *  *  *  *  *
    *  *  *  *  *  *  *  *  *
    *  *  *  *  *  *  *  *
       *  *  *  *  *  *  *
       *  *  *  *  *  *  *
          *  *  *  *  *  *
          *  *  *  *  *  *
          *  *  *  *  *
             *  *  *  *
             *     *
             *     *
             *
             *
             *
             *
             *
             *
             *
             *
             *
             *
             *

As per OP's request,

line = ' '.join(' *' if v else '  ' for v in d.values())

uses conditional expression. For example, output = 'a' if x else 'b' sets output as 'a' if x is True, and 'b' if x is False.

In this case, python sees v. If v is non-zero, it is "truthy", so ' *' if v else ' ' for v equals to ' *'. If v is zero, then v is "falsy", so it equals to ' '. In other words, the line is equivalent to

temp = []
for v in d.values():
    if v != 0:
        temp.append(' *')
    else:
        temp.append('  ')

line = ' '.join(temp)

CodePudding user response:

To make it vertical you need to check each number separately, make a for loop that counts from 1 to 100 (which are the minimum and the maximum prospects) to check in each number if that number reaches this value and a nested for loop to check each num if it reaches it....something like:

list = [num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12]
for i in range(1,100):
  line = []
  for x in list:
    if x >= i:
      line.append("*")
    else:
      line.append(" ")
  for element in line:
    print(element, end="")
  • Related