Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 9th, 2013, 03:17 PM
 ColinC02
Registered User

Join Date: Jun 2013
Posts: 4
Time spent in forums: 3 h 2 m 34 sec
Reputation 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
June 9th, 2013, 10:21 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
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!

#3
June 9th, 2013, 10:37 PM
 ColinC02
Registered User

Join Date: Jun 2013
Posts: 4
Time spent in forums: 3 h 2 m 34 sec
Reputation 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?

#4
June 10th, 2013, 03:14 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
Study a phone book and see if you can think of an algorithm to produce columns in a similar manner.

#5
June 10th, 2013, 09:54 PM
 ColinC02
Registered User

Join Date: Jun 2013
Posts: 4
Time spent in forums: 3 h 2 m 34 sec
Reputation 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()

#6
June 10th, 2013, 10:25 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,218
Time spent in forums: 1 Month 3 Weeks 2 Days 18 h 51 m 46 sec
Reputation Power: 455
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.

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > Question regarding 'chr' and 'ascii' tables