不能用'float'类型的非int来乘以序列 [英] can't multiply sequence by non-int of type 'float'

查看:1391
本文介绍了不能用'float'类型的非int来乘以序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我得到错误不能乘以类型为'float'的非整型序列?

level:beginner

  def nestEgVariable(salary,save,growthRates):
SavingsRecord = []
fund = 0
depositPerYear = salary * save * 0.01
for i in growth比率:
fund = fund *(1 + 0.01 * growthRates)+ depositPerYear
SavingsRecord + = [fund,]
返回SavingsRecord


print nestEgVariable(10000,10,[3,4,5,0,3])

感谢
Baba

解决方案

  for i in growthRates:
fund = fund *(1 + 0.01 * growthRates)+ depositPerYear

应该是:

  for i in growthRates:
fund = fund *(1 + 0.01 * i )+ depositPerYear

您将使用growRates列表对象乘以0.01。乘以一个整数的列表是有效的(它是超负荷的语法糖,它允许你创建一个带有其元素引用副本的扩展列表)



示例:

 >>> 2 * [1,2] 
[1,2,1,2]


level: beginner

why do i get error "can't multiply sequence by non-int of type 'float'"?

def nestEgVariable(salary, save, growthRates):
    SavingsRecord = []
    fund = 0
    depositPerYear = salary * save * 0.01
    for i in growthRates:  
        fund = fund * (1 + 0.01 * growthRates) + depositPerYear
        SavingsRecord += [fund,]
    return SavingsRecord 


print nestEgVariable(10000,10,[3,4,5,0,3])

thanks Baba

解决方案

for i in growthRates:  
    fund = fund * (1 + 0.01 * growthRates) + depositPerYear

should be:

for i in growthRates:  
    fund = fund * (1 + 0.01 * i) + depositPerYear

You are multiplying 0.01 with the growthRates list object. Multiplying a list by an integer is valid (it's overloaded syntactic sugar that allows you to create an extended a list with copies of its element references).

Example:

>>> 2 * [1,2]
[1, 2, 1, 2]

这篇关于不能用'float'类型的非int来乘以序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆