0

and thanks in advance. I am working on a program, and during it, I am allowing for the change of some connections within the program itself. As such, I wrote the following code to facilitate this:

n = {'car': 'gate'} #this also exists for all the other directions
directions = ['n', 's', 'e', 'w', 'ne', 'nw', 'sw', 'se', 'u', 'd'] 
roomto = input("\nROOM TO CHANGE CONNECTIONS FOR (A) # ").lower()
toroom = input("\nROOM IT SHOULD LEAD TO (B) #").lower()   
toroomd = input("\nWHAT DIRECTION SHOULD LEAD FROM A TO B? # ").lower()
toroomb = input("\nWHAT DIRECTION SHOULD LEAD FROM B TO A? # ").lower()
if(toroomd in directions) and (toroomb in directions):
   statusd = 'status' + toroomd
   statusb = 'status' + toroomb
   toroomd[roomto] = toroomd
   toroomb[toroom] = roomto
   print("Success!")

However, it gives me the following error (ignore the line number, that's just where it is in the larger program):

TrTraceback (most recent call last):
File "python", line 1885, in <module>
TypeError: 'str' object does not support item assignment

I have read a bit, and have tried to wrap the toroomd[roomto] = toroomd in a list, but that just gives me a new error about list cant being around a str and having to be around a float or a splice.

Any suggestions, or is this just not possible?

Adam_O
  • 19
  • 5
  • 1
    `toroomd[roomto] = toroomd`: you cannot write in a string slice (and your index is not even an integer). Unclear what you're asking. – Jean-François Fabre Jan 04 '17 at 16:01
  • 1
    Possible duplicate of [Replacing instances of a character in a string](http://stackoverflow.com/questions/12723751/replacing-instances-of-a-character-in-a-string) – David Zemens Jan 04 '17 at 16:03
  • I should have probably added -- and will now -- that the toroomd/toroomb represent directories earlier on (for example: n = {'car': 'gate'}) – Adam_O Jan 04 '17 at 16:04
  • 1
    @DavidZemens Possibly, but he's not even doing that. The line `toroomd[roomto] = toroomd` is trying to index into a string *x* with another string *y* and set the nonsensical result of that to the original string *x*. I'm very confused on what the intention is. – Tagc Jan 04 '17 at 16:05
  • OK, but `toroomd` and `toroomb` are *strings*, so this line (and the next line) are nonsense. What are you actually trying to do here? `toroomd[roomto] = toroomd`? – David Zemens Jan 04 '17 at 16:09

1 Answers1

1

Strings in python are immutable.
You can't change it.
Only create new one.

toroomd[roomto] = toroomd
must be
toroomd = toroomd[:roomto] + toroomd + toroomd[roomto+1:]

GoodWasHere
  • 138
  • 6