任何有关修复下面复杂arg的指针"小时",我正在
SyntaxError:语法无效
我想要的只是减少尽可能多的代码。 我希望它在最后给出一个对应于i值的值。
我的代码在
失败了 (('($200 Hr) estimated at' if i ==1 ) or ('($130 Hr) estimated at' if i == 2 ) or ('($120 Hr) estimated at' if i == 3 ) or ('($125 Hr) estimated at' if i == 4) or ('($90 Hr) estimated at' if i == 5))
代码:
brs_items_list.append({
'desc' : kwargs["brs_d_{}".format(i)],
'hours' : kwargs["brs_hrs_{}".format(i)]+(' hour' if float(kwargs["brs_hrs_{}".format(i)])<2 else ' hours')+ (('($200 Hr) estimated at' if i ==1 ) or ('($130 Hr) estimated at' if i == 2 ) or ('($120 Hr) estimated at' if i == 3 ) or ('($125 Hr) estimated at' if i == 4) or ('($90 Hr) estimated at' if i == 5)) ,
'cost' : "${:,}".format(round(float(kwargs["brs_c_{}".format(i)]), 2))
})
答案 0 :(得分:2)
为什么不使用字典?
{1:'($200 Hr) estimated at', 2:'($130 Hr) estimated at', 3:'($120 Hr) estimated at', 4:'($125 Hr) estimated at', 5:'($90 Hr) estimated at'}[i]
更好的是,你有很多重复的文字,所以你可以这样做:
'($%d Hr) estimated at' % {1:200, 2:130, 3:120, 4:125, 5:90}[i]
答案 1 :(得分:0)
我认为我需要
brs_items_list.append({
'desc' : kwargs["brs_d_{}".format(i)],
'hours' : kwargs["brs_hrs_{}".format(i)]+(' hour' if float(kwargs["brs_hrs_{}".format(i)])<2 else ' hours')+ (('($200 Hr) estimated at' if int(i) ==1 else '') or ('($130 Hr) estimated at' if int(i) == 2 else '') or ('($120 Hr) estimated at' if int(i)== 3 else '' ) or ('($125 Hr) estimated at' if int(i) == 4 else '') or ('($90 Hr) estimated at' if int(i) == 5 else '')) ,
'cost' : "${:,}".format(round(float(kwargs["brs_c_{}".format(i)]), 2))
})