Python - User input for name of a variable to pass as an argument to a function

后端 未结 3 1365
轻奢々
轻奢々 2021-01-14 23:06

I have a question here which is based around user input to scripts and passing this user-input to functions.

I have a script, within which I have defined a function.

3条回答
  •  攒了一身酷
    2021-01-14 23:50

    Use a dictionary, mapping strings to objects:

    tbl1, tbl2 = [1 ,2 ,3], [4 ,5 ,6]
    args = {'tbl1': tbl1 ,"tbl2" :tbl2}
    # show tables ......
    
    inp = input("Choose table")
    
    
    def foo(var):
        print(var)
    
    
    foo(args[inp])
    

    You will want to do error checking to make sure the user actually enters something valid.

提交回复
热议问题