Concatenation of tuples

淺唱寂寞╮ 提交于 2020-02-23 10:14:32

问题


  1. Normal text:

    • I'm having some problems with coding on python 3.2.1. Actually I'm taking online lectures that are on python 2.5.
  2. Here is the code:

    x = 100
    divisors = ()
    for i in range(1,x):
        if x%i == 0:
            divisors = divisors + (i)
    
  3. on running the program, following error appears:

    divisors = divisors + (i)  
    TypeError: can only concatenate tuple (not "int") to tuple
    

回答1:


(1) is not a tuple, its just a parenthesized expression. To make it a tuple, add a trailing comma, (1,)




回答2:


Try to use this instead:

divisors.append(i)

Edit:

divisors = []

since you can't append on tuples.



来源:https://stackoverflow.com/questions/7192391/concatenation-of-tuples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!