Enter to raw_input automatically

前端 未结 4 1794
傲寒
傲寒 2020-12-17 23:42

Just as an example, it might seem illogical. I have a get_name function as below, and wanted to write a automated script to call this function and enter to the raw_inp

4条回答
  •  粉色の甜心
    2020-12-17 23:59

    Another option is to make the input function a parameter, defaulting to raw_input:

    def get_name(infunc=raw_input):
        name = infunc("Please enter your name : ")
        print "Hi " + name
    

    Then for testing purposes you can pass in a function that does whatever you need:

    get_name(lambda prompt: "John Smith")
    

提交回复
热议问题