我在为3.x版本编写python脚本时遇到了一些问题。我需要编写一个python,在哪里可以计算所请求主体的年度利息。如果没有提供信用信息并且可以支付初始预付款,则将存在违约利率。我开始但是我遇到了错误。
由于
import sys
import argparse
parser = argparse.ArgumentParser(description="extracting command-line para")
parser.add_argument(
help = 'Total Request',
nargs='+',
dest = 'TotalRequest',
default = [],
type = str
)
parser.add_argument(
'-c',
'--credit',
help = 'Calc interest rate',
dest = 'credit',
default = '650',
type = int)
parser.add_argument(
help = 'List of principals',
nargs='+',
dest = 'principal',
default = [],
type = str
)
arg = parser.parse_args()
if arg.credit >= 700:
rate = 4.5
elif arg.credit >= 600 and arg.credit < 700:
rate = 5.0
else:
rate = 6.0
sum = 0
for p in arg.principal:
sum = sum + int(p)
interest = sum * rate / 100
print ("Total Request: ", sum)
print ("Down payment: ",)
print ("Principal: ", sum)
print ("Interest rate: ", rate)
print ("Annual interest: ", interest)
答案 0 :(得分:0)
当您在此处设置默认值时:
parser.add_argument(
help = 'List of principals',
nargs='+',
dest = 'principal',
default = [],
type = str
)
这实际上并不是指定一个默认变量,而是add_argument函数的一个参数,它为参数赋予一个默认值。因此,对于上面的示例,您的主要值将是
principal = []
考虑到上述因素,当您尝试访问默认变量时,它是未知的。
for p in arg.principal:
sum = default + int(p)