Reputation:
I need to make a code where the user enters two numbers (for example, 7 and -7) and the code will print a list of numbers between it. (-7, -6, -5, -4, -3, -2, -1, 0 ,1, 2, 3, 4, 5, 6, 7)
I have only got the input so far:
number = int(input('Enter the first number: '))
number = int(input('Enter the second number: '))
It should look like this:
Enter the first number: 7
Enter the second number: -7
(-7, -6, -5, -4, -3, -2, -1, 0 ,1, 2, 3, 4, 5, 6, 7)
Upvotes: 1
Views: 1354
Reputation: 972
Define it as a function. It gives additional functionality to enable the user to also control the step size between the two input numbers.
import numpy
start = input('Enter start number:')
stop = input('Enter stop number:')
step = input('Enter required between two numbers:')
def list_of_num(start, stop, step):
return numpy.arange(int(start), int(stop), int(step))
print(list_of_num(start, stop, step))
Upvotes: 0
Reputation: 71610
Use range
:
number = int(input('Enter the first number: '))
number2 = int(input('Enter the second number: '))
print(list(range(number2, number + 1)))
Example output:
Enter the first number: 7
Enter the second number: -7
[-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7]
If you want a tuple
:
number = int(input('Enter the first number: '))
number2 = int(input('Enter the second number: '))
print(tuple(range(number2, number + 1)))
Example output:
Enter the first number: 7
Enter the second number: -7
(-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7)
Upvotes: 1