2

我需要编写一个程序,给出下表的输出:

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  

任何帮助将不胜感激,尽管我想要求不要给出完整的答案,而是给出提示,所以对我来说有一些挑战。谢谢。

4

5 回答 5

6

ord和函数将chr帮助您:

ord('a')  # 97
chr(97)   # 'a'

添加到一个range,你就开始炖了!

于 2013-03-28T21:07:09.310 回答
4
for i in range(32,128):
    print (i, chr(i))

或者更接近你想要的:

#!/usr/bin/python3
def f(x,y):
    for i in range(x,y):
        print ('%3d '%i,end=''),
    print()
    for i in range(x,y):
        print ('%3s '%chr(i),end='')
    print()
for x in range(32,128,16):
    f(x,x+16)
于 2013-03-28T21:08:04.283 回答
1
print '''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'''
于 2013-03-28T21:06:02.097 回答
1

提示:在给定范围的循环中使用循环,从而将行区分为案例。转换后的字符可以作为字符串一起添加,并且只有在添加了所有字符串后才打印该行。

完整的代码是:

for i in range (2,14): 
 #range(1,13 would have been correct as well, but I want to use the parity)
if i%2==0: #use even numbers for "chr..." line
  a="chr:  "
  for j in range(int((i/2-1)*16+32),int((i/2-1)*16+48)):
#range is a bit complicated due to the i-range
     b=str(chr(j))
#the following ifs are used to regulate space depending on character length
     if len(b)==1:
        s="   "
     if len(b)==2:
        s="  "
     if len(b)==3:
        s=" "
     a=a+b+s #add new characters with space to the previous ones
  print(a)
if i%2==1: #use odd numbers for asc:... line
  a="asc: "
  for j in range(int(((i-1)/2-1)*16+32),int(((i-1)/2-1)*16+48)):
     b=str(j) #in this line you need only the numbers
#the following ifs are used to regulate space depending on character length
     if len(b)==1:
        s="   "
     if len(b)==2:
        s="  "
     if len(b)==3:
        s=" "
     a=a+b+s
  print(a)
于 2017-03-16T07:49:01.083 回答
0

一个好朋友给了我这个提示,它很有效!剩下要做的就是调整占位符:

    for i in range (0, 12):
       if i%2 ==0:
         content ="chr:"
         for j in range (32, 48):
            content=content+"   "+str(chr(j+(i//2)*16))
          print (content)    
        if i%2 ==1:
          content = "asc:"
          for j in range (32, 48):
             content=content+"  "+str(j+(i//2)*16)
          print (content) 
于 2020-07-04T17:05:34.063 回答