允许范围函数计算非数字表达式
我正在使用 makearange 函数输入数组的开始、停止和增量范围,即
User = raw_input('Enter start,[stop],[increment]: ').split(',')
makearange = lambda a: numpy.arange(int(a[0]),int(a[1]),int(a[2]))
x = makearange(User)
,但是我也使用这些数字来运行程序来创建输入数字的平方和立方体的数组。我在无限 while 循环上运行这个程序,只有当用户按下返回键时才会停止。所以我尝试了
if User == "":
Break
哪个可行,除了这会导致错误,因为 makearange 函数仅评估整数而不是用户输入的返回键。我怎样才能让它理解这种类型的输入? 谢谢
I am using a makearange function to input a start, stop, and increment range for an array i.e.
User = raw_input('Enter start,[stop],[increment]: ').split(',')
makearange = lambda a: numpy.arange(int(a[0]),int(a[1]),int(a[2]))
x = makearange(User)
However I am also using these numbers to run a program to create arrays of the squares and cubes of the inputted numbers. I am running this program on an infinite while loop, which only stops when the user hits the return key. So I have tried
if User == "":
Break
Which would work except this results in an error because the makearange function only evaluates integers and not the user input of the return key. How can I get it to understand this type of input?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要立即尝试用逗号分割
User
,而是先测试它是否为空字符串:PS。
输入start,[stop],[increment]
表明stop
和increment
是可选的。这是否意味着如果只给出一个参数,您希望范围从给定的数字开始并无限增加?这不适用于 numpy.arange 。也许您的意思是start
是可选的,而stop
是必需的。这与 numpy.arange 已经工作的方式完美契合。Instead of immediately trying to split
User
on commas, test if it is the empty string first:PS.
Enter start,[stop],[increment]
suggests thatstop
andincrement
are optional. Does that mean if only one argument is given, you want the range to start from the given number and increase infinitely? That's won't work withnumpy.arange
. Perhaps you meant thestart
to be optional, and thestop
to be required. That would fit perfectly with the waynumpy.arange
already works.当然,您可以想出比数组长度更好的验证
of course you can come up with a better validation than the length of the array