首页 > 解决方案 > 我不断收到类型错误,不知道为什么

问题描述

def printWord(a,b):
    a= raw_input ("What would you like me to say?")
    b= raw_input ("How many times would you like me to say it?")
    int(float(b))
    for i in range(b):
        print a

这段代码不断给我这个错误:

line 10, in printWord
  for i in range(b):
TypeError: range() integer end argument expected, got str.

标签: pythontypeerror

解决方案


你对这条线有正确的想法:

int(float(b))

但这并没有就地改变 b 。你必须保留结果。用这个:

b = int(float(b))

推荐阅读