gpt4 book ai didi

python - 涉及列表的格式问题

转载 作者:行者123 更新时间:2023-11-30 23:26:16 25 4
gpt4 key购买 nike

我有这个函数,它将名称作为输入,将其放入列表中,然后对其运行 ord() 。但是,我遇到了一些(我认为的)格式问题。我试图让它看起来像这样:

b = (ascii value)
a = (ascii value)
t = (ascii value)
m = (ascii value)
a = (ascii value)
n = (ascii value)

我的名称显示正确,但 ascii 值显示如下:

b = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
t = [98, 97, 116, 109, 97, 110]
m = [98, 97, 116, 109, 97, 110]
a = [98, 97, 116, 109, 97, 110]
n = [98, 97, 116, 109, 97, 110]

不确定我哪里出错了,下面是我为此编写的代码:

def x():
name = requestString("name")
usersName = list(name)
ascii = [orc(c) for c in usersName]
for name in name:
print name, "=", ascii

谢谢!

编辑:谢谢,真的很感激。现在就找出我错在哪里!

最佳答案

以下是对您出错的地方的一些回顾:

def x():
name = requestString("name")
usersName = list(name)
ascii = [orc(c) for c in usersName] # here's the list
for name in name:
print name, "=", ascii # and you're printing it here everytime

你可以像这样更Python地修复:

def x():
name = requestString("name")
# usersName = list(name) # no need for this line, you can iterate over the string
ascii = [orc(c) for c in name] #so this is just name
for i, c in enumerate(name): # use c for your character var name,
print c, "=", ascii[i] # and enumerate provides the index

由于您没有返回任何内容,因此没有必要创建列表,您不妨即时提供 ord(c):

def print_ords_of_word(name):
for c in name:
print c, '=', ord(c)

关于python - 涉及列表的格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22622517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com