so I'm new to the language so I've trying to do a vertical bar graph as shown below without luck, so far the only thing i've managed to do is to make it horizontally like this:

***********
*******
****
*********
****

which i do it by
for v in d:
print(" * " * v)

however I've been trying to transpose the results but i still dont get it.

this is what i need:

d = {0: 1, 1: 10, 2: 150, 3: 200, 4:80}

|                         *
|    *                *  *
|    *          *     *  *
|    *      *   *    *   *
|    *      *   *    *   *
------------------------------
0     1    2   3   4   5

can anybody point me on the right path?

thanks!

thanks for your response, however the code mentioned in that thread does not work that's why im asking:

def ascii_barchart( lst ):
    for row in range( max(lst), 0, -1 ):
        for elem in lst:
            if elem >= row: 
                print "# ",
            else: 
                print "  ",
        print

l = [20, 10, 15, 5]
ascii_barchart( l )

that will produce an output as follows:


#
#
#
#

#
#

#
#
#
#
#


so im not looking for that.. thanks again.. let me know where i should be looking at.!

Hmm, tested mawe's code and it worked just fine ...

# mawe's vertical barschart

def ascii_barchart(lst):
    for row in range(max(lst), 0, -1):
        for elem in lst:
            if elem >= row: 
                print "# ",
            else: 
                print "  ",
        print

lst = [2, 4, 7, 10, 8, 5, 3, 1]
ascii_barchart(lst)

'''my output (python27) ...
         #             
         #             
         #  #          
      #  #  #          
      #  #  #          
      #  #  #  #       
   #  #  #  #  #       
   #  #  #  #  #  #    
#  #  #  #  #  #  #    
#  #  #  #  #  #  #  # 
'''

yeah, thanks for looking into this, in python 3 the print " ", is print(" ",) however it still gives the output as i said.. not sure what are the big differences between python 2.7 and 3 though.

see below on python 3.0

lst = [2,4,7,10,8]
def ascii_barchart(lst):
    for row in range(max(lst), 0, -1):
        for elem in lst:
            if elem >= row: 
                print("# ",)
            else: 
                print("  ",)
        print()

ascii_barchart(lst)
  
  
  
# 
  

  
  
  
# 
  

  
  
  
# 
# 

  
  
# 
# 
# 

  
  
# 
# 
# 

  
  
# 
# 
# 

  
# 
# 
# 
# 

  
# 
# 
# 
# 

# 
# 
# 
# 
# 

# 
# 
# 
# 
#

Edited 5 Years Ago by sys73r: n/a

The correct print() function for Python3 should look like this ...

# mawe's vertical barschart for Python3

def ascii_barchart(lst):
    for row in range(max(lst), 0, -1):
        for elem in lst:
            if elem >= row: 
                print("# ", end=' ')
            else: 
                print("  ", end=' ') 
        print()

lst = [2, 4, 7, 10, 8, 5, 3, 1]
ascii_barchart(lst)

'''my output (python32) ...
         #             
         #             
         #  #          
      #  #  #          
      #  #  #          
      #  #  #  #       
   #  #  #  #  #       
   #  #  #  #  #  #    
#  #  #  #  #  #  #    
#  #  #  #  #  #  #  # 
'''

The correct print() function for Python3 should look like this ...

# mawe's vertical barschart for Python3

def ascii_barchart(lst):
    for row in range(max(lst), 0, -1):
        for elem in lst:
            if elem >= row: 
                print("# ", end=' ')
            else: 
                print("  ", end=' ') 
        print()

lst = [2, 4, 7, 10, 8, 5, 3, 1]
ascii_barchart(lst)

'''my output (python32) ...
         #             
         #             
         #  #          
      #  #  #          
      #  #  #          
      #  #  #  #       
   #  #  #  #  #       
   #  #  #  #  #  #    
#  #  #  #  #  #  #    
#  #  #  #  #  #  #  # 
'''

thanks man, that works:

the new documentation about print and trailing is as follows:

Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline


thank you guys!

This question has already been answered. Start a new discussion instead.