如何将函数参数的字符串值一起存储到单个变量中?

How do I store the string values of a functions parameters together into a single variable?

我试图将 strings_length3 函数中参数的三个字符串值一起存储在一个变量中。然后我想通过打印存储它们的变量的长度来打印这三个存储字符串的总长度。

#defining the function
def strings_length3(age, sex, name):
    print(len(age) + len(sex) + len(name))

#calling the function; the 3 string values to be stored in the variable tot
strings_length3("36", "male", "Xi")
    tot = 36 + male + Xi
    print(len(tot))

非常感谢任何帮助。

你的函数应该return(不打印)你想要的长度。然后,将 return 值分配给您的变量。尝试:

def strings_length3(age, sex, name):
    return len(age) + len(sex) + len(name)

tot = strings_length3("36", "male", "Xi")
print(tot)

或者,您可以 return 连接字符串并将 len 仅应用于函数调用的结果:

def strings_length3(age, sex, name):
    return age + sex + name

tot = strings_length3("36", "male", "Xi")
print(len(tot))

您没有return从函数中获取值。用每个参数的长度调用 return 而不是 print 语句,您可以将变量分配给函数调用