开发者

Get User to Input range in Python

I am trying to ask the user for a range (for an array) i.e vari开发者_运维知识库ables start, stop and increment. I am having difficulty asking for each variable without breaking up my string. So far I have this code:

x=numpy.arange(input('Enter Start:'), input('Enter Stop:'),input ('Enter increment:'))

However I would like it to read: 'Enter Start, Stop, Increment:' and allow the user to input three numbers e.g. 2, 10, 2 rather than inputting them one at a time.

I would be grateful for any advice


>>> start, stop, inc = raw_input('Enter start, stop, increment:').split(',')
Enter start, stop, increment:0, 5, 2
>>> print start, stop, inc
0  5  2

Keep in mind that those are strings, use int to convert them


The user has to confirm his entry by hitting the ENTER key. input() automatically outputs that as a linebreak, so you can't have it all in one line with 3 calls to input(). You could use one input and ask for all 3 values:

>>> userrange = input("Please input start, stop and increment: ")
Please input start, stop and increment: 1,2,3
>>> userrange
(1, 2, 3)


>>> import numpy as np
>>> makearange = lambda a: np.arange(int(a[0]),int(a[1]),int(a[2]))
>>> x = makearange(raw_input('Enter start,stop,increment: ').split(','))
Enter start,stop,increment: 2,100,10
>>> x
array([ 2, 12, 22, 32, 42, 52, 62, 72, 82, 92])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜