Question

I need some code that allows a user to input a number (ie. 5) that creates a list of all the numbers leading up to that number excluding zero (ie. [1,2,3,4,5])

Was it helpful?

Solution

>>> range(1,int(raw_input('Number: '))+1)
Number: 5
[1, 2, 3, 4, 5]

OTHER TIPS

In [11]: L = range(1, int(raw_input("Enter a number: "))+1)
Enter a number: 5

In [12]: L
Out[12]: [1, 2, 3, 4, 5]
while True:
     try:
        v = int(raw_input('Number: '))
     except:
        print 'Invalid number!!'
     else:
        print range(1,v)
        break

This will prevent the user from inputing an invalid number.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top