3

对于教程,我需要使用嵌套的 for 循环在 python 上输出下表:

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'''

由于这是初学者课程,我们还没有学习def。这个问题已经在以前的帖子中回答过,但答案使用 def 而我不能

到目前为止,我的代码如下所示:

X=0
for Rows in range(0,12,2):
   X=X+1
   if Rows%2==0:
      print('chr:',end="")
      for chrColumns in range (32,48):
        print('%4s'%chr(chrColumns+(X-1)*16), end="")
   print()
   if Rows%2==1:
      print('asc:',end="")
      for ascColumns in range (16,32):
      print(ascColumns+(X-1)*16,end="")
   print()

我找不到“chr:”行与“asc:”行交替的方法。请帮我

4

4 回答 4

5

这样的事情怎么样?

for c in range(32, 128, 16):
    #print chr line
    for c1 in range(c, c+16):
        # print chr
    #print asc line
    for c2 in range(c, c+16):
        # print asc
于 2013-08-20T16:52:00.053 回答
1
for c in range(32, 128, 16):
   print('chr:',end="")
   for c1 in range(c, c+16):
      print('%4s'%chr(c1), end="")
   print()
   print('asc:',end="")
   for c2 in range(c, c+16):
      print('%4s'%c2, end="")
   print()
于 2013-08-21T16:35:46.853 回答
0

这是我不优雅的解决方案:

P = 32
for line in range (0,6):
 print ("chr:", end = "  ")
 for chrCols in range (P,P+16):
   print (chr(chrCols), end="   ")
 print ('\n' "asc: ", end = "")
 for ascCols in range (P,P+15):
   if ascCols < 100:
    print (ascCols, end="  ")
   if ascCols > 99:
    print (ascCols, end=" ")
 print(ascCols+1)
P = P + 16
于 2017-07-26T21:59:43.070 回答
0
servers = ["server1", "server2", "server3"]
table_column = 2
row_format ="{:>15}" * (table_column)
print(row_format.format("Server", "Status"))
print ("--"*18)
for server in servers:
    print(row_format.format(server, "code"))

输出

在此处输入图像描述

于 2020-07-28T12:26:04.573 回答