#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Question regarding 'chr' and 'ascii' tables


    Hi everyone, I'm new to the forums and am also pretty new to Python. I'm actually in a class right now, and am having trouble with my latest assignment. I'm not looking for an answer of code, I just need help with the logic if possible because I'm struggling to grasp it.

    I'm supposed to use the builtin 'chr' function to make an AsciiChart function that returns a string that when printed, is an ascii chart, six entries across. I'm only supposed to use the ascii values from 32 to 126, inclusive.

    First of all, I'm having trouble understanding what exactly the 'chr' function does? Then, I'm really struggling to come up with some basic logic to map out before attacking the program.

    Please let me know if you have any ideas.
    Thank you very much.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    The interactive nature of python is really great. You can quickly test ideas. what's ASCII? ASCII history
    Code:
    >>> chr(100)
    'd'
    >>> help(chr)
    Help on built-in function chr in module __builtin__:
    
    chr(...)
        chr(i) -> character
        
        Return a string of one character with ordinal i; 0 <= i < 256.
    
    >>> 
    >>> 
    >>> 
    >>> 
    >>> 
    >>> 
    >>> 
    >>> for i in range(32,126+1): print(i,chr(i))
    ... 
    (32, ' ')
    (33, '!')
    (34, '"')
    (35, '#')
    (36, '$')
    (37, '%')
    #...
    (118, 'v')
    (119, 'w')
    (120, 'x')
    (121, 'y')
    (122, 'z')
    (123, '{')
    (124, '|')
    (125, '}')
    (126, '~')
    >>>
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Thanks for the response!
    I took your idea and basically reformatted it into a "for loop"...now I'm just trying to figure out how to make it 6 columns across.

    I'm getting the correct data back from my "for loop" with values for both the number and 'chr'(value), but it's all in two vertical columns due to the way I inserted a tab in my print statement.

    I need to somehow cut the result so that it is spread into four more vertical columns.
    Any ideas?
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Study a phone book and see if you can think of an algorithm to produce columns in a similar manner.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0
    Here's what I came up with. How does this look?



    Code:
     
    def GiveAsciiChart5():     
        count = 3          
        for value in range(32, 127):         
            count += 1         
            if count % 4 == 0:              
                print "\n"         
            print " ASCII Value: ", (value),        
            print " Character: ", chr(value),          
    
    def Main():     
        GiveAsciiChart5()  
    
    if __name__ == '__main__':     
        Main()
  10. #6
  11. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,929
    Rep Power
    481
    Originally you said you needed 6 columns. There's also a lot of repetition, is that why someone invented column headings? These comments sound too nasty. You succeeded with the spirit of the project, and I'm no final arbiter on table layout.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo