2 回答
TA贡献1847条经验 获得超7个赞
我正在更新您的部分代码:
string2 = input("enter several integers separated by a comma ") # 0,1,2
string3 = string2.split(',') # string3 is a list of numbers but as strings
# ['0','1','2']
string3=[int(num) for num in string3] # [0,1,2]
for num in string3:
print(x.get(num))# will return the value corresponding to the key
TA贡献1820条经验 获得超2个赞
使用这段代码,简单地做同样的事情,但在最后做一个循环,并return在函数中有一个:
string = input("Please enter a name> ")
def enumerate_string(string):
s = {}
for i,letter in enumerate(string):
s[i] = letter
return s
x = enumerate_string(string)
string2 = input("enter several integers separated by a comma ")
string3 = string2.split(',')
for i in string3:
if int(i) in x:
print(x[int(i)])
示例输出:
Please enter a name> John Doe
enter several integers separated by a comma 0,1,2
J
o
h
添加回答
举报