-1

I have some code like:

G = 'Hello!'
variable = input('Enter a letter').upper()

I then want the program to print 'Hello!' when the user enters the letter G. How can I do this? Is exec() useful for the task? I saw some mentions of it, but I didn't understand how to use it.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
George Willcox
  • 677
  • 12
  • 30
  • [**Do not ever use `eval` (or `exec`) on data that could possibly come from outside the program in any form. It is a critical security risk. You allow the author of the data to run arbitrary code on your computer.**](https://stackoverflow.com/questions/1832940/why-is-using-eval-a-bad-practice) – Karl Knechtel Jul 04 '22 at 23:24

1 Answers1

0

Use locals():

a = 'foo'
locals()['a']
'foo'
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Benjamin
  • 3,350
  • 4
  • 24
  • 49