【问题标题】:how to take a String argument within a function returned as a tuple如何在作为元组返回的函数中获取字符串参数
【发布时间】:2018-07-31 01:05:31
【问题描述】:

好的,所以我想要做的是循环遍历字符串参数,然后尝试将其添加到元组中,但是我无法做到这一点,我目前正在使用 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) 

【问题讨论】:

  • 你能提供一个示例输入和所需的输出吗?
  • 是的,我刚刚对其进行了编辑并添加了更多细节以及结果的图片

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


【解决方案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', '中国电机工程学报', '27000', '乔伊鲍勃', '史密斯')

【讨论】:

  • 我说了同样的话哈。似乎他希望客户调用一个函数
  • 你在哪里写的 if 语句会被算作程序的主体吗?
猜你喜欢
  • 2018-03-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-17
  • 1970-01-01
  • 1970-01-01
  • 2012-03-27
  • 1970-01-01
相关资源
最近更新 更多