简体   繁体   中英

Need to print out the Python3 Character Map (asc:32 - 127)

I need to write a program that prints out the chr lines from the table below (including 32 and 127 which is just whitespace). I was able to successfully print all the chr from the table but it prints each character on separate lines. It keeps coming up as incorrect output so I assume it needs to be on same line? I cannot figure out how to print the chr for asc:32 - 127 on the same line with just a space between characters and get the answer correct? Any advice is appreciated!

chr:      !   "   #   $   %   &   '   (   )   *   +   ,   -   .   / 
asc: 32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47 
chr:  0   1   2   3   4   5   6   7   8   9   :   ;   <   =   >   ? 
asc: 48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63 
chr:  @   A   B   C   D   E   F   G   H   I   J   K   L   M   N   O 
asc: 64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79 
chr:  P   Q   R   S   T   U   V   W   X   Y   Z   [   \   ]   ^   _ 
asc: 80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95 
chr:  `   a   b   c   d   e   f   g   h   i   j   k   l   m   n   o 
asc: 96  97  98  99  100 101 102 103 104 105 106 107 108 109 110 111
chr:  p   q   r   s   t   u   v   w   x   y   z   {   |   }   ~     
asc: 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 

Here is what I have that works but its still being marked wrong:

letter = str(' ')
order = ord(letter)
for item in range(32, 128):
   if order != 128:
      order = item 
      print(chr(order))
   else:
      break

You could keep all characters in a list until you want to have a line break:

chr_pairs = []
for i in range(32, 129):
    if i % 16 == 0:
        if chr_pairs:
            print('chr: ' + '  '.join(str(x[0]).zfill(3) for x in chr_pairs))
            print('asc: ' + '  '.join(' %s ' % x[1] for x in chr_pairs))
        chr_pairs = []
    chr_pairs.append((i, chr(i)))

Out:

chr: 032  033  034  035  036  037  038  039  040  041  042  043  044  045  046  047
asc:       !    "    #    $    %    &    '    (    )    *    +    ,    -    .    / 
chr: 048  049  050  051  052  053  054  055  056  057  058  059  060  061  062  063
asc:  0    1    2    3    4    5    6    7    8    9    :    ;    <    =    >    ? 
chr: 064  065  066  067  068  069  070  071  072  073  074  075  076  077  078  079
asc:  @    A    B    C    D    E    F    G    H    I    J    K    L    M    N    O 
chr: 080  081  082  083  084  085  086  087  088  089  090  091  092  093  094  095
asc:  P    Q    R    S    T    U    V    W    X    Y    Z    [    \    ]    ^    _ 
chr: 096  097  098  099  100  101  102  103  104  105  106  107  108  109  110  111
asc:  `    a    b    c    d    e    f    g    h    i    j    k    l    m    n    o 
chr: 112  113  114  115  116  117  118  119  120  121  122  123  124  125  126  127
asc:  p    q    r    s    t    u    v    w    x    y    z    {    |    }    ~     

Note : There is asciitable , which pretty-prints tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM