这是我的代码:
Adherent = "a person who follows or upholds a leader, cause, etc.; supporter; follower."
word=raw_input("Enter a word: ")
print word
当我运行此代码并输入Adherent时,会生成Adherent一词.我如何才能将Adherent的定义改为弹出?
解决方法:
您可以(应该)使用dictionary将单词映射到定义:
>>> word_dct = {"Adherent" : "a person who follows or upholds a leader, cause, etc.; supporter; follower."}
>>> word=raw_input("Enter a word: ")
Enter a word: Adherent
>>> word_dct[word]
'a person who follows or upholds a leader, cause, etc.; supporter; follower.'
>>>
创建动态变量是一种糟糕且有潜在危险的做法.它不仅很容易丢失它们,而且如果使用exec或eval创建它们,则存在执行任意代码的风险.有关更多信息,请参见Why should exec() and eval() be avoided?.即使访问本地或全局也通常不赞成使用列表或字典.
标签:python,variables,raw-input
来源: https://codeday.me/bug/20190717/1490835.html