Add a string to a tuple

This is the place for queries that don't fit in any of the other categories.

Add a string to a tuple

Postby bluepanido » Thu Jul 18, 2013 9:18 am

Can anybody please help with this basic problem. I am a complete beginner. I want to add a string to a tuple. Do I need to create a new tuple each time a string is added. The answer is probably a simple one. Here is my code, all help appreciated. Thanks.

Code: Select all
num = int(input("How many items would you like to add to your inventory: "))

count = 0
stuff = ()

while count != num:
    thing = input("Type an item to add to your inventry: ")
    stuff += thing
    count += count

print("You now have the following items: ", stuff)
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK

Re: Add a string to a tuple

Postby metulburr » Thu Jul 18, 2013 9:30 am

you are trying to add 'thing' which is a string, and 'stuff' which is a tuple, which in the first place they are two different data types. 2) tuples are immutable, which means you cannot change the content. You can only concatenate two tuples together, like so:

Code: Select all
>>> t1 = (1,)
>>> t1
(1,)
>>> t2 = (2,)
>>> t2
(2,)
>>> t1 + t2
(1, 2)
>>> t1[0] = 'fail'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>>


but this is essentially what your doing:
Code: Select all
>>> t1 += 'test'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: can only concatenate tuple (not "str") to tuple


Your going to have to make a new tuple and reassing it back.
Code: Select all
>>> t1 = ('test', )
>>> t1
('test',)
>>> t1[0] = 'failing'
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> t1
('test',)
>>> t1 = ('test2',)
>>> t1
('test2',)


but you could very well use a list, which is mutable and will allow you to change
Code: Select all
>>> l = []
>>> l.append('test')
>>> l
['test']
>>> l[0] = 'reassigning'
>>> l
['reassigning']
>>>


im not sure why you are storing the value in the tuple as there is only one index, you might as well store it as an integer, adn increment it. but i dont know what you are doing, so if you needed it within something like a list:
Code: Select all
>>> l = [0]
>>> l
[0]
>>> l[0] += 1
>>> l
[1]
>>>
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1319
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Add a string to a tuple

Postby bluepanido » Thu Jul 18, 2013 9:34 am

So do I need to create a new tuple each time, with the contents of the old tuple (stuff), plus the string the user has entered (thing). If so, can you tell me how to do this please?

thanks
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK

Re: Add a string to a tuple

Postby metulburr » Thu Jul 18, 2013 9:41 am

I beleive you would want lists instead, but if you insist on tuples:
Code: Select all
tup = (24,)
tup = (tup[0] + 1,)
print(tup)
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1319
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Add a string to a tuple

Postby bluepanido » Thu Jul 18, 2013 1:09 pm

okay. Thanks for taking the time to reply. The next chapter of the book I'm learning from deals with lists and dictionaries, so I will probably find out how to do what I'm trying to do in there. The mutability of a list will allow me to add to it I think.

thanks again
bluepanido
 
Posts: 7
Joined: Thu Jul 11, 2013 12:18 pm
Location: UK


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 2 guests