python调用函数,参数作为用户在字典中的输入?

2024-04-19 08:11:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一本字典

d={}

用户输入

no=raw_input("Enter number: ")
x=raw_input("Enter string: ")
y=raw_input("Enter string: ")
z=raw_input("Enter string: ")



d[no]=send(x,y,z)

def send(x,y,z):
    print x,y,z

这可能吗?你知道吗

我试过了,但当我打印字典时,它就输出了

{1: None}

我想要这样的东西

 d{
    1:send(x,y,z),
    2:send(x,y,z),
    3:send(x,y,z)
  }

其中x,y,z是用户输入。你知道吗


Tags: no用户nonesendnumberinputstringraw
3条回答

你的send函数没有意义。你可以这样做:

d[no] = (x,y,z)

如果您想分配并打印到控制台,那么我想您可以:

d[no] = send(x,y,z)

def send(x,y,z):
    print x,y,z
    return x,y,z

但这很奇怪。你知道吗

使用return而不是print

def send(x,y,z):
    return x, y, z

您将得到:

d = {
    1: (x, y, z)
    2: (x, y, z)
    3: (x, y, z)
}

如果您真的想在字典中看到"send(x, y, z)",请使用:

def send(x,y,z):
    return "send({0}, {1}, {2})".format(x, y, z)

只是返回值而不是打印它。你知道吗

def send(x, y, z):
    return x, y, z

相关问题 更多 >