先谢谢你。我正在编写一个程序,在编写过程中,我允许程序本身中某些连接的更改。为此,我编写了以下代码:
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!")
但是,它给了我以下错误(忽略行号,这正是它在较大程序中的位置):
TrTraceback (most recent call last):
File "python", line 1885, in <module>
TypeError: 'str' object does not support item assignment
我读了一点,并尝试将toroomd[roomto]=toroomd包装在一个列表中,但这给了我一个新的错误,即列表不能围绕str,必须围绕float或拼接。你知道吗
有什么建议吗,或者这是不可能的?你知道吗
python中的字符串是不可变的。
你不能改变它。
只创建一个新的。你知道吗
toroomd[roomto] = toroomd
必须
toroomd = toroomd[:roomto] + toroomd + toroomd[roomto+1:]
相关问题 更多 >
编程相关推荐