How to get value of variable entered from user input?

前端 未结 3 1678
余生分开走
余生分开走 2020-12-06 02:53

I am trying to create a basic menu that checks to see if the variable entered matches a defined variable. If the variable is defined get the data of the defined variable.

3条回答
  •  伪装坚强ぢ
    2020-12-06 02:58

    You'll need to use locals()[Choose_Item] if you want to choose a variable whose name is what the user produced.

    A more conventional way to do this, though, is to use a dictionary:

    items = {
        'Item1': 'bill',
        'Item2': 'cows',
        'Item3': 'abcdef',
    }
    

    ... and then the value you want is items[Choose_Item].

提交回复
热议问题