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 m开发者_开发百科akearange 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
Instead of immediately trying to split User
on commas, test if it is the empty string first:
import numpy as np
import sys
user_input = raw_input('Enter [start,] stop[, increment]: ')
if user_input = '':
sys.exit()
else:
x=np.arange(*map(int,user_input.split(',')))
PS. Enter start,[stop],[increment]
suggests that stop
and increment
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 with numpy.arange
. Perhaps you meant the start
to be optional, and the stop
to be required. That would fit perfectly with the way numpy.arange
already works.
makearange = lambda a: numpy.arange(int(a[0]),int(a[1]),int(a[2])) if len(a) == 3 else None
of course you can come up with a better validation than the length of the array
精彩评论