num = 0
for x in s:
if x in 'aeiou':
num += 1
print 'Number of vowels:' + str(num)
单词出现次数 or只记一次,利用In重复计算
num = 0
i = 0
for i in range(len(s)):
if s[i:i+3] == 'bob':
num += 1
print 'Number of vowels:' + str(num)
利用切片计算词组出现次数
balance = #欠款
annualInterestRate = #年利率
monthlyPaymentRate = #月最低还款
mma = annualInterestRate/12.0#月利率
month = 0#月 可删除
mmp = 0#月还款 可删除
tp = 0#总还款
for month in range(1,13):
mmp = balance * monthlyPaymentRate
#月还款=欠款*月最低还款
rb = (balance-mmp)*mma + (balance - mmp)
#优化rb = (balance-mmp)*(1+mma)
print 'Month:' + str(month)
print 'Minimum monthly payment:' + str(round(mmp,2))
print 'Remaining balance:' + str(round(rb,2))
balance = rb
#欠款=待还款
tp += mmp
#总还款累加
print 'Total paid:' + str(round(tp,2))
print 'Remaining balance:' + str(round(rb,2))
balance = #欠款
annualInterestRate = #年利率
mma = annualInterestRate/12.0 #月利率
mmp = 0#初始还款额
while True:
balancex = balance#设定循环初始欠款,每次重新开始
for month in range(12):
balancex -= mmp #本月欠款额
balancex = balancex*(1+mma) #下月欠款额
if balancex <= 0.01:#当欠款为负数结束循环
print ' Lowest Payment ' + str(mmp)#输出当前月还款额
break#结束循环
else:
mmp += 10#条件不成立下月还款增加
balance = #欠款
annualInterestRate = #年利率
mplb = balance / 12.0#最小还款
mpub = (balance*((1 + annualInterestRate/12.0)**12.0))/12.0#最大还款
while True:
balancex = balance#设定循环初始欠款,每次重新开始
mpj = (mplb+mpub)/2.0#二分法
for month in range(12):
balancex = (balancex-mpj)*(1 + annualInterestRate/12.0)
if abs(balancex) <= 0.01:
print ' Lowest Payment ' + str(round((mpj),2))
break
elif balancex > 0.01:
mplb = mpj
else:
mpub = mpj