I am currently doing miniscript that is sending documents to a specific path based on a code I enter.
The code is basic, but I get an error which I really don't understand.
When I run the following code:
en = "English"
source = eval(input("Source language?\n"))
print (source)
If I hit 'en' I get English without any errors.
But if I run the same without input:
en = "English"
eval(en)
I get "name 'English' is not defined"
Basically, I want to use some of eval() functions in my code without the input() function. Where I am wrong?
CodePudding user response:
eval
expects a string as its argument. By passing it en
, you're giving it the string "english"
. Instead, you probably want
eval("en")
However, using eval
is generally bad practice. I'd look into alternative ways of doing what you're after.
CodePudding user response:
Usually there is never any need to use eval
in Python
:
languages = {'fr': 'french', 'gr': 'greek', 'en': 'english'}
source = languages.get(input("Source language?\n"), 'Undefined language!')
print(source)
Out:
Source language?
gr
greek