python - 如何在以元组形式返回的函数中获取字符串参数

标签 python loops for-loop tuples nested-loops

好吧,我想要做的是循环遍历字符串参数,然后尝试将其添加到元组中,但是我无法做到这一点,并且我当前正在使用 print(tuple()) 方法。 我也想使用 return 而不是 print。

非常感谢任何帮助,我也是这方面的初学者!

currently what results I get, doubled words and of course because I used a for loop it adds each letter separately, so far this is the closest I've gotten

def part1 (payroll, dept, salary, firstname, lastname):
    tuple1 = ()


    for i in payroll:
        payroll = payroll + i
    for i in dept:
        dept = dept + i
    for i in salary:
        salary = salary + i
    for i in firstname:
        firstname = firstname + i
    for i in lastname:
        lastname = lastname + i

    print(tuple(payroll))
    print(tuple(dept))
    print(tuple(salary))
    print(tuple(firstname))
    print(tuple(lastname))

part1('13214', 'CSEE', '27000', 'joey bob', 'Smith')

我想要的输出是

(13214 CSEE 27000 joey bob Smith) 

最佳答案

def part1(partonelist):
    return tuple(partonelist)

if __name__ == "__main__":
    part1_list = ['13214', 'CSEE', '27000', 'joey bob', 'Smith']
    my_tup = part1(part1_list)
    print(my_tup)

将它们放在列表中会更干净一些...然后只需调用元组函数即可。

这将打印: ('13214', 'CSEE', '27000', '乔伊·鲍勃', '史密斯')

关于python - 如何在以元组形式返回的函数中获取字符串参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51604147/

相关文章:

android - 检查图像是否为黑色

python - 有没有办法将Python脚本转换为Linux上可执行的Windows程序

停止和等待算法的 Python 实现

java - 我在制作带有循环的菱形形状时遇到问题

c - 给定 10 个输入数字并存储在一个数组中,我如何检查下一个值是否与上一个不同?

python - Pandas :将列值更改超过一定数量

python - Collectd - Perl/Python 插件 - 注册功能不起作用

c - 不知道如何正确使用getchar函数/C

javascript - 如何动态结束加载动画

JavaScript。当包含在函数中时,我的 for 循环在一次迭代后停止