为什么我们不能在python的for循环中传递范围内的算术表达式

Sun*_*ula 0 python range python-3.x

我收到一个错误,因为“浮动”对象不能解释为整数

num=5
for x in range(num/2):
     print("hello")
Run Code Online (Sandbox Code Playgroud)

par*_*ver 6

你应该使用整数除法

num=5
for x in range(num//2):
     print("hello")
Run Code Online (Sandbox Code Playgroud)

range不支持float输入。如果要使用浮动值,请考虑使用numpy's arange