我正在编写一个应该打印的程序:
A abcdefghijklmnopqrstuvwxyz
B bcdefghijklmnopqrstuvwxyz
C cdefghijklmnopqrstuvwxyz
D defghijklmnopqrstuvwxyz
E efghijklmnopqrstuvwxyz
F fghijklmnopqrstuvwxyz
G ghijklmnopqrstuvwxyz
H hijklmnopqrstuvwxyz
I ijklmnopqrstuvwxyz
J jklmnopqrstuvwxyz
K klmnopqrstuvwxyz
L lmnopqrstuvwxyz
M mnopqrstuvwxyz
N nopqrstuvwxyz
O opqrstuvwxyz
P pqrstuvwxyz
Q qrstuvwxyz
R rstuvwxyz
S stuvwxyz
T tuvwxyz
U uvwxyz
V wxyz
X xyz
Y yz
Z z
我为该程序编写了以下代码,但它没有打印出我想要的内容。这就是我为该计划所写的内容:
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for k in range(len(alphabet)):
for j in range(len(alphabet)):
print(alphabet[j-k],end='')
print('\n')`
并打印出来:
abcdefghijklmnopqrstuvwxyz
zabcdefghijklmnopqrstuvwxy
yzabcdefghijklmnopqrstuvwx
xyzabcdefghijklmnopqrstuvw
wxyzabcdefghijklmnopqrstuv
vwxyzabcdefghijklmnopqrstu
uvwxyzabcdefghijklmnopqrst
tuvwxyzabcdefghijklmnopqrs
stuvwxyzabcdefghijklmnopqr
rstuvwxyzabcdefghijklmnopq
qrstuvwxyzabcdefghijklmnop
pqrstuvwxyzabcdefghijklmno
opqrstuvwxyzabcdefghijklmn
nopqrstuvwxyzabcdefghijklm
mnopqrstuvwxyzabcdefghijkl
lmnopqrstuvwxyzabcdefghijk
klmnopqrstuvwxyzabcdefghij
jklmnopqrstuvwxyzabcdefghi
ijklmnopqrstuvwxyzabcdefgh
hijklmnopqrstuvwxyzabcdefg
ghijklmnopqrstuvwxyzabcdef
fghijklmnopqrstuvwxyzabcde
efghijklmnopqrstuvwxyzabcd
defghijklmnopqrstuvwxyzabc
cdefghijklmnopqrstuvwxyzab
bcdefghijklmnopqrstuvwxyza
abcdefghijklmnopqrstuvwxyz
我需要帮助来弄清楚我做错了什么以及我需要做些什么才能打印出我想要打印的代码。
答案 0 :(得分:1)
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
for k in range(len(alphabet)):
for j in range(len(alphabet) - k):
print(alphabet[j+k],end='')
print('\n')
我希望它会对你有所帮助。
答案 1 :(得分:1)
您的问题有一个更清洁,更短的解决方案:
alphabet = "abcdefghijklmnopqrstuvwxyz"
for i, letter in enumerate(alphabet):
print(letter.upper(), alphabet[i:])
我建议您阅读Python切片(from the doc或here on Stack)。
答案 2 :(得分:0)
这是一种更加“pythonic”的方式,它也利用了Python 3.6中的新f字符串:
alphabet = 'abcdefghijklmnopqrstuvwxyz'
for i,k in enumerate(alphabet):
print(f'{k.upper()} {alphabet[i:]}')
enumerate
为索引提供当前迭代的值。.upper()
给出了字符串的大写版本。[i:]
切片表示法返回一个从索引i开始直到字符串结尾的子字符串。答案 3 :(得分:0)
这可以使用python slice工具在两行代码中完成,也可以大写索引:
a = 'abcdefghijklmnopqrstuvqxyz'
add = ''
for i in range(26):
print(a[i].capitalize(), a[-26:i+1])