Mikey
Mikey

Reputation: 13

ASCII Table confusion

for my class we have to create a program that shows the ASCII characters from ! to ~. I have the program running but it has to be 10 characters per line, and my program is adding 80-84. Can anyone help, I don't know what is wrong.

for i in range(33, 126, 10): #determines range of characters shown

   for characters in range(i, i+10): #determins characters per line
      print('%2x %-4s'%(characters, chr(characters)) , end="") #prints number and ASCII symbol
   print()


Upvotes: 0

Views: 29

Answers (1)

jsbueno
jsbueno

Reputation: 110186

Your problem is that when the ouuter loop stops, at "123", it will still run the whole inner loop, from "123" to "123 + 10" - as those characters are not printable (unicode codepoints 0x80 - 0xa0 have no associated glyph), you only see the numbers.

You have to add an extra condition to stop printing - either at your program as it is, inside the inner loop, or reformat your program to use one single loop, and a counter variable to do the line breaks.

for i in range(33, 126, 10): #determines range of characters shown

   for characters in range(i, i+10): #determins characters per line
      if characters >= 0x80:
          break
      print('%2x %-4s'%(characters, chr(characters)) , end="") #prints number and ASCII symbol
   print()

Upvotes: 1

Related Questions