[英]Python - Passing Functions with Arguments as Arguments in other Functions
[英]Passing arguments for functions while opening python file from terminal in Linux
我正在嘗試通過Linux中的終端打開我擁有的python腳本,格式如下所示:
$ python file_name.py a,b,c
在這里,a,b,c是我在為程序編寫的代碼中使用的變量。 目前,我正在通過要求用戶使用命令var_name = input(“ Enter input for a:”)輸入輸入
但是我想按上述格式打開文件本身時傳遞這三個變量的值。 我需要利用類來做到這一點嗎?
我寫的代碼是:
print"Hello!"
circle = []
n = input("Enter the number of elements in the circle 'N' :")
for i in range(0,n):
circle.append(i)
print "The circle being formed is : " + str(circle)
m = input("Enter the value of M : ")
k = input("Enter the value of K : ")
index = m-1
while (len(circle)>k) :
del circle[index]
index = index + (m-1)
n = n - 1
index = (index)%n
print str(circle)
如果您使用的是Python 2.7(很有可能),則可以嘗試argparser
import argparse
parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('integers', metavar='N', type=int, nargs='+',
help='an integer for the accumulator')
parser.add_argument('--sum', dest='accumulate', action='store_const',
const=sum, default=max,
help='sum the integers (default: find the max)')
args = parser.parse_args()
print args.accumulate(args.integers)
並運行:
$ python prog.py 1 2 3 4
4
$ python prog.py 1 2 3 4 --sum
10
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.