简体   繁体   English

如何使用字符串作为变量名

[英]How do I use a string as a name of variable

I want to use this program:我想使用这个程序:

name = input('name: ') # mark
age = int(input('age')) #23
name = age

mark = 23

I know that I can use a dictionary, but is there a way to create an actual variable with string?我知道我可以使用字典,但是有没有办法用字符串创建实际变量?

I have absolutely no reason to do this and I understand it's pretty much unusable, but I wanted to try something new and came across a problem that could be fixed like this (but shouldn't be)我完全没有理由这样做,我知道它几乎无法使用,但我想尝试一些新的东西,并遇到了一个可以像这样修复的问题(但不应该)

I found the answer to my question pretty quickly, but I'll leave it here for someone else:我很快就找到了我的问题的答案,但我会把它留给其他人:

name = input('name: ') # mark
age = input('age') #23
with open("temp.py",'w') as f:
    f.write(name+" = "+age)
from temp import *

mark = 23

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM