Question

Okay, so I'm trying to write a simple program that gives me both the positive and negative range of the number given by the user.

For example, if the user gives number 3 then the program should prints out -3 -2 -1 0 1 2 3

I've tried thinking but just can't think how to get the negative range outputs. But the code I got below only gives me the positive range outputs, so I was thinking may I need to do to make it gives me both the positive and negative outputs.

 s = int(input("Enter a number "))
 for i in range(s+1):
     print i+1
Was it helpful?

Solution

Range() can take two parameters: range(start, end), where start is inclusive, and end is exclusive. So the range you want for 3 is: range(-3, 4). To make it general:

s = int(input("Enter a number "))
for i in range(-s, s+1):
    print i
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top