如何在以元组形式返回的函数中采用String参数

时间:2018-07-31 01:05:31

标签: python loops for-loop tuples nested-loops

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

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

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) 

1 个答案:

答案 0 :(得分:1)

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”,“ joey bob”,“史密斯”)