简体   繁体   English

如何在 Python 中运行多个输入?

[英]How to run multiple inputs in Python?

I have an extensive Python script, that runs one input each time.我有一个广泛的 Python 脚本,每次运行一个输入。

input('Write the file name:')
... 

And because I have a lot going on in my script (and don't want to change the operations in it), I wanted to know if (and how) there is a way to give my script multiple inputs.而且因为我的脚本中有很多事情要做(并且不想更改其中的操作),我想知道是否(以及如何)有一种方法可以为我的脚本提供多个输入。

So that I give the script a list of inputs and it runs input1 , when this one ends, runs the input2 , etc...所以我给脚本一个输入列表,它运行input1 ,当这个结束时,运行input2等等......

Is there a way to achieve this in Python?有没有办法在 Python 中实现这一点?

To make it more automatic you can use the following code:为了使其更加自动化,您可以使用以下代码:

x, y, z = input("input1: "), input("input2: "), input("imput3: ") x, y, z = 输入(“输入 1:”),输入(“输入 2:”),输入(“输入 3:”)

Your situation can be solve using sys.argv .您的情况可以使用sys.argv来解决。

To run your code, you would type in python script.py input1 input2 input3 .要运行您的代码,您需要输入python script.py input1 input2 input3

Then, use sys.argv as follows:然后,使用sys.argv如下:

import sys
sys.argv  #returns ["script.py", "input1", "input2", "input3"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM