Hello everyone!

I have task to write a program that would read a data from any txt file and visualize it on a bar chart using turtle module. I have to structure the program using modules and functions for nice design. The program must be fit for any other txt file that I would be able to later read.
The sample data file would look like this:

Student MarkA MarkB MarkC MarkD MarkF
Alex 5 2 1 0 1
Mark 3 5 1 0 0
Costa 7 4 0 0 3
Rita 0 6 2 2 4
Mary 4 2 3 1 0
John 3 4 2 1 0


I would greatly appreciate anyone's help in this. I hope someone will find the time to help out.
Thanks again.

Here some short try on this, but I am only beginning to learn myself the the turtle module.

It does do some rudimentary scaling adapting to number of students and number of their marks.

If you use this code, send me your final code in return and clearly mark the origin of the code you borrow from this code.

#!/usr/bin/python
"""       turtle_histograms.py

            input in file data.txt same directory as this file
            sample input from request in daniweb:

Student MarkA MarkB MarkC MarkD MarkF
Alex 5 2 1 0 1
Mark 3 5 1 0 0
Costa 7 4 0 0 3
Rita 0 6 2 2 4
Mary 4 2 3 1 0
John 3 4 2 1 0

You are free to use this code in your solutions, if you keep the
attribution to original author

Written by Tony Veijalainen, 2010

"""

## little dangerous convenience import mixing the namespace of the program
from turtle import  * 

## h should be from real window size
h=500

def histo(d,w, color1, scale = 10):
    """ histogram with
           bar length d,
           width w
           [and maximum d walue scale] """
    
    mult = h // scale ## reducing the height when max mark bigger, integer division
    fill(True)
    down()
    color(color1)
    forward(w)
    left(90)
    color(color1)
    forward(d * mult)
    left(90)
    forward(w)
    ## half done: print the value as number in black
    color('black')
    write(d)
    color(color1)
    left(90)
    color(color1)
    forward(d  *  mult)
    left(90)
    fill(False)
    ## some spacing and axis
    color('black')
    forward(w * 14 // 9)

def main():
    reset()
    ## no delay
    delay(0)
    hideturtle()
    ## move near left side and down of the default window
    up()
    left(180)
    fd(h // 2)
    left(90) ## down
    fd(h // 2)
    left(90) ## down bottom complete 360 degrees
    down()
    ## chosen colors, must be more than maximum numbers per line
    colors =  ('red','blue','yellow','green','gray','gold','magenta')
    f =  open('data.txt','r')
    li =  [x.split() for x in f]

    count =  len(li.pop(0)) - 1 ## count numbers and drop the header for now
    colors =  colors[:count]

    ## max of marks as integer numbers each line, ignore name
    m =  map(lambda x: max(int(y) for y in (x[1:])),li)
    ## max of all students marks as numbers, ignore name
    m =  max([int(x) for x in m[1:]])
    ## this should be calculated from window width instead of given in the future
    width =   210 // (count * (len(li) - 1)) 
    print 'Width set to',width
    print 'Name and ',count,'numbers. Max mark',m 
    
    for info in li:
        print info
        ## go 20 down to write name and come back up
        up()
        right(90)
        forward(20)
        left(90)
        write(info.pop(0))
        left(90)
        forward(20)
        right(90)
        ## write the histograms for student
        for c in colors:
            histo(int(info.pop(0)),width,c ,scale =  m)
        ## separation line in black
        down()
        fd(5)
        left(90)
        color('black')
        fd(h)
        up()
        backward(h)
        right(90)
        fd(5)
    
    ht()
    f.close()
    return "Done!"

if __name__  == '__main__':
    main()
    mainloop()
commented: super +13

P.S. This turtle graphics code (http://www.daniweb.com/forums/thread273625.html) has bug in separating the numbers, you must think how to fix the problem (hint: test with this data from other thread and see why the max is wrong)

Country Gold Silver Bronze Total
US 9 15 13 37
GE 10 13 7 30
CA 14 7 5 26
NO 9 8 6 23
AU 4 6 6 16
RF 3 5 7 15
KO 6 6 2 14
CH 5 2 4 11
SW 5 2 4 11
FR 2 3 6 11

tonyjv, very nice code!

Unfortunately somewhat wasted on a do my homework for me character. Please stay in touch with this forum, your skills are much needed.

I did leave something to fix there in the code, which takes some effort to understand the code. Realistically it is accomplshement for somebody who does not only copy the other's work but takes effort to fix the bug in the code. (of course he can just copy the code and think it is enough with the bug on)

Also I am a beginner myself, as I had not used the things I replied to myself. Of course I learned programming with Pascal and other languages since 1981...

Cheers,
Tony
P.S. See Papert on 'learning to think is debugging'
http://www.papert.org/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.