"can only concatenate tuple"? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: "can only concatenate tuple"? (/thread-11740.html) |
"can only concatenate tuple"? - wlsa - Jul-24-2018 The question wants me to create a function that takes a tuple as an input, and returns a new tuple as an output, where every other element of the input tuple is copied, starting with the first element. def oddTuples(aTup): i=0 what_I_want=() while i<len(aTup): if i%2==0: final=final + aTup[i] i+=1 print(what_I_want) oddTuples((1, 2, 3, 4 ,5,))expected output: (1, 3, 5) actual output: typeError: can only concatenate tuple (not "int") to tuple I'm confused about the basis of the question because I learned a tuple is immutable so how could I do this? RE: "can only concatenate tuple"? - buran - Jul-24-2018 first of all, read this https://python-forum.io/Thread-Basic-Never-use-for-i-in-range-len-sequence it also apply to your use of while loop there are at least three ways to do it
RE: "can only concatenate tuple"? - ichabod801 - Jul-24-2018 You could also put the integer you are trying to add to the tuple into a tuple. Two tuples add and return a tuple. |