python - How to use raw input to define a variable -
python - How to use raw input to define a variable -
here code:
adherent = "a person follows or upholds leader, cause, etc.; supporter; follower." word=raw_input("enter word: ") print word
when run code , input adherent, word adherent produced. how can have definition of adherent pop instead?
you can (should) utilize dictionary words mapped definitions:
>>> word_dct = {"adherent" : "a person follows or upholds leader, cause, etc.; supporter; follower."} >>> word=raw_input("enter word: ") come in word: adherent >>> word_dct[word] 'a person follows or upholds leader, cause, etc.; supporter; follower.' >>>
creating dynamic variables bad , potentially unsafe practice. not easy lose track of them, if utilize exec
or eval
create them, run risk of executing arbitrary code. see why should exec() , eval() avoided? more information. accessing locals
or globals
frowned upon in favor of using list or dictionary.
python variables raw-input
Comments
Post a Comment