Python 类型错误:范围()整数结束参数预期,得到浮点数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28357754/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:09:18  来源:igfitidea点击:

TypeError: range() integer end argument expected, got float?

pythonfloating-point

提问by Chef1075

I know this has been asked before, but the answers did not help me :/

我知道以前有人问过这个问题,但答案对我没有帮助:/

I created a function that runs a for loop over the squared max of the inputs, and by all accounts my code is correct...and yet it still asks for float inputs.

我创建了一个函数,它在输入的平方最大值上运行 for 循环,并且从所有帐户来看,我的代码是正确的……但它仍然要求浮点输入。

def spiral(X, Y):

x = y = 0
dx = 0
dy = 0
count = 0

for i in range(max(X, Y)**2):
    if (-X/2.0 < x <= X/20) and (-Y/2.0 < y <= Y/2.0):
        print (x, y)

    if x == y or (x < 0 and x == -y) or (x > 0 and x == 1-y):
        dx, dy = -dy, dx

    x, y = x+dx, y+dy

print spiral(3.0,3.0)

打印螺旋(3.0,3.0)

And I get this error: TypeError: range() integer end argument expected, got float.

我得到这个错误: TypeError: range() integer end argument expected, got float.

But I put 3.0 when I try and print the function...so what am I missing?

但是当我尝试打印该函数时,我放了 3.0 ......那我错过了什么?

Thanks :)

谢谢 :)

采纳答案by Avinash Raj

Like others said in the comment, the problem is mainly because of the float value in range function. Because range function won't accept float type as an argument.

就像其他人在评论中所说的那样,问题主要是因为 range 函数中的浮点值。因为 range 函数不接受 float 类型作为参数。

for i in range(max(int(X), int(Y))**2):