获取一个 char 并将其从 char 打印到 'a'并反转它应该是递归的
此代码应采用 char 作为参数,并按字母顺序将该 char 打印为“a”,然后反向打印为 char。
>>> characters('d')
d c b a b c d
这是我到目前为止所写的,但它不是正确的输出
def characters(char):
numb=ord(char)
while numb>ord('a'):
>> print chr(numb),
numb=numb-1
return
>>> characters('h')
g f e d c b a
this code should take a char as an argument and print out that char in alphabetically order to 'a' and reverse to char.
>>> characters('d')
d c b a b c d
this is what Ii wrote so far but it is not the correct output
def characters(char):
numb=ord(char)
while numb>ord('a'):
>> print chr(numb),
numb=numb-1
return
>>> characters('h')
g f e d c b a
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者
or
好吧,就目前情况而言,你已经成功了一半。现在你只需要弄清楚如何让你的信恢复麻木。
为了使其在字母表中倒退,您可以使用
numb=numb-1
。那么,为了让它在字母表中前进,相反的做法是什么?然后你可以将其放入另一个循环中。Well, you're halfway there as it stands. Now you just have to figure out how to take numb back to your letter.
In order to make it go backwards in the alphabet, you're using
numb=numb-1
. So in order to make it go forward in the alphabet, what would be the opposite of that? Then you could put that in another loop afterwards.