Ad
Variables In Input Questions
So, I know that if you do:
variable1 = 10
variable2 = variable1
then variable2
will be 10. But if you do:
variable1 = 10
and then ask for an input
:
variable2 = input("...")
and type variable1
as the input, then variable2
will be 'variable1'
instead of 10. How can I change this so that variable2
equals variable1
after inputting the string variable1
?
Ad
Answer
If you're sure that you really want to do this, you can use eval
, but this is a bad practice, and leaves you susceptible to arbitrary code injection.
>>> variable1 = 10
>>> variable2 = eval(input())
variable1
>>> variable2
10
A slightly better approach might be to use the globals
or locals
builtins, but this is still a bad practice.
>>> variable2 = globals()[input()]
variable1
>>> variable2
10
Ad
source: stackoverflow.com
Related Questions
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Can't turn off Javascript using Selenium
- → WebDriver click() vs JavaScript click()
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module
Ad