开发者

Concatenation of tuples

  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) is not a tuple, its just a parenthesized expression. To make it a tuple, add a trailing comma, (1,)


Try to use this instead:

divisors.append(i)

Edit:

divisors = []

since you can't append on tuples.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜