【问题标题】:Python using for loop to turn a list into AscIIPython 使用 for 循环将列表转换为 AscII
【发布时间】:2020-07-06 16:56:25
【问题描述】:

所以我现在一直在研究这个问题,但目前我无法弄清楚,我需要使用 for 循环的函数将数字列表转换为字符串。不完全确定我做错了什么。任何帮助,将不胜感激。 这是列表

[67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115、105、110、103、32、97、32、108、111、111、112、33]

它看起来像它,但在一行中,而不是在一行中,例如“恭喜你解压了 使用循环的消息:" https://i.stack.imgur.com/9ee8k.png

【问题讨论】:

  • 嗨,Fefe,欢迎来到该网站。请将您的代码作为文本包含在问题中,而不是作为编辑器的屏幕截图。如果可以的话,也强烈推荐将输出包含为文本(屏幕截图适用于图形输出,但对于像你这样的输出,文本会更好)。
  • 感谢您的意见!我下次会第一次发帖!感谢大家的帮助!

标签: python for-loop ascii python-3.8


【解决方案1】:

应该是:

def convert2string():
    for i in list1:
        print(chr(i), end="")

convert2string()

Congrats you have unpacked the message using a loop!

【讨论】:

  • 不要忘记函数的参数
【解决方案2】:

这是解决您的问题的示例代码 sn-p。您可以通过添加更多标点符号和空白字符来更新它。希望对您有所帮助。

data = [67, 111, 110, 103, 114, 97, 116, 115, 32, 121, 111, 117, 32, 104, 97, 118, 101, 32, 117, 110, 112, 97, 99, 107, 101, 100, 32, 116, 104, 101, 32, 109, 101, 115, 115, 97, 103, 101, 32, 117, 115, 105, 110, 103, 32, 97, 32, 108, 111, 111, 112, 33]

white_spaces=[9,10,13,32]
for i in data:
  if (i >=65 and i <=90)or (i>=97 and i<=122) or (i in white_spaces):
    print("%c"%(i), end='')

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 2019-02-26
    • 1970-01-01
    • 2020-03-29
    相关资源
    最近更新 更多