允许字符串变为变量以设置值

2024-10-04 11:23:57 发布

您现在位置:Python中文网/ 问答频道 /正文

先谢谢你。我正在编写一个程序,在编写过程中,我允许程序本身中某些连接的更改。为此,我编写了以下代码:

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或拼接。你知道吗

有什么建议吗,或者这是不可能的?你知道吗


Tags: toin程序inputlowerleadshoulddirection