Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python原始输入_Python_Python 2.7_Raw Input - Fatal编程技术网

Python原始输入

Python原始输入,python,python-2.7,raw-input,Python,Python 2.7,Raw Input,我用的是py 2.7。我对python完全陌生,我的意思是三天。不过,我正在努力降低这个基本能力,这样我就可以在学习本书的过程中创建一些脚本。我想做的是创建一个脚本,当在函数、方法和模块之间选择列表时,它会将列表打印到屏幕上。我所知道要做的就是这样,但如果有人能帮助我让脚本接受函数、方法或模块作为打印列表的输入,我会非常感激,并且会更快地学习python。提前谢谢 raw_input ("Choose functions, methods, or modules: ') 当然,我也知道如何列出

我用的是py 2.7。我对python完全陌生,我的意思是三天。不过,我正在努力降低这个基本能力,这样我就可以在学习本书的过程中创建一些脚本。我想做的是创建一个脚本,当在函数、方法和模块之间选择列表时,它会将列表打印到屏幕上。我所知道要做的就是这样,但如果有人能帮助我让脚本接受函数、方法或模块作为打印列表的输入,我会非常感激,并且会更快地学习python。提前谢谢

raw_input ("Choose functions, methods, or modules: ')
当然,我也知道如何列出:

functions= [
...some functions
]
modules = [
...some modules
]
methods = [
...some methods
]

我不知道该怎么做,就是把它全部放在一个可执行脚本中,该脚本将接受这三个选项作为原始输入。请记住,这不是py 3,所以输入不是我要找的。

下面是一个示例,说明如何接受3种不同的用户输入选择。如果用户没有键入函数、方法或模块,它将要求他们再次选择

choice = None
while choice not in ['functions', 'methods', 'modules']:
    choice = raw_input('Choose functions, methods, or modules: ')
然后根据他们输入的内容,如果在三个选项中,它将只执行他们键入的选项。这几乎适用于任何情况

   def userInput():
       variable_21 = raw_input("Type functions, methods, or modules: ")
       if variable_21 == "functions":
           doFunction()
           print functions
       elif variable_21 == "methods":
           print methods
       elif variable_21 == "modules":
           print modules
       else:
           print "Sorry, you didn't type a valid value."
           userInput()

对于初学者来说,这是一个学习python的好方法。请访问www.codecademy.com。他们有非常直接、循序渐进的指示

下面是一个示例,说明如何接受3种不同的用户输入选择。如果用户没有键入函数、方法或模块,它将要求他们再次选择

然后根据他们输入的内容,如果在三个选项中,它将只执行他们键入的选项。这几乎适用于任何情况

   def userInput():
       variable_21 = raw_input("Type functions, methods, or modules: ")
       if variable_21 == "functions":
           doFunction()
           print functions
       elif variable_21 == "methods":
           print methods
       elif variable_21 == "modules":
           print modules
       else:
           print "Sorry, you didn't type a valid value."
           userInput()

对于初学者来说,这是一个学习python的好方法。请访问www.codecademy.com。他们有非常直接、循序渐进的指示
functions = ['sum', 'id', 'str', 'list']
modules = ['random', 'numpy', 'scipy', 'os']
methods = ['some_other_stuff']

answer = dict()
answer['functions'] = functions
answer['modules'] = modules
answer['methods'] = methods

while True:
    choice = raw_input('Choose between functions, methods, or modules: ')
    if choice in answer.keys(): # ['functions', 'modules', 'methods']
        print answer[choice]

你可以考虑把它们映射成字典。所有直接代码

functions = ['sum', 'id', 'str', 'list']
modules = ['random', 'numpy', 'scipy', 'os']
methods = ['some_other_stuff']

answer = dict()
answer['functions'] = functions
answer['modules'] = modules
answer['methods'] = methods

while True:
    choice = raw_input('Choose between functions, methods, or modules: ')
    if choice in answer.keys(): # ['functions', 'modules', 'methods']
        print answer[choice]

我试着使用那个代码,我合并了你的两篇文章(没有搞错代码),我不知道错误是什么,因为提示关闭的速度太快,我读不懂,但我甚至没有选择函数、方法或模块的选项。@user3101688有趣。这段代码完全适合我。我尝试过使用这段代码,我合并了你的两篇文章(没有搞错代码),我不知道错误是什么,因为提示关闭的速度太快,我无法阅读,但我甚至没有选择函数、方法或模块的选项。@user3101688有趣。这段代码完全适合我。我只是将代码复制并粘贴到记事本++文件中,并将其保存为.py,然后运行它。不工作。我用了第一个例子。谢谢你在codeacademy上的留言,我会试试看你的回复是@codehorse,是吗?:)那我是,忘了,让我试试你的剧本吧,我不得不承认我不完全理解代码,因为可以说我已经三天大了,但它起作用了,而且在我讲到那部分课程之前它会起作用。非常感谢,基本上是这样。检查链接。我只是将代码复制并粘贴到记事本++文件中,并将其保存为.py,然后运行它。不工作。我用了第一个例子。谢谢你在codeacademy上的留言,我会试试看你的回复是@codehorse,是吗?:)那我是,忘了,让我试试你的剧本吧,我不得不承认我不完全理解代码,因为可以说我已经三天大了,但它起作用了,而且在我讲到那部分课程之前它会起作用。非常感谢,基本上是这样。检查链接。