0

I am trying to create a bounded rectangle using a random number of points that are provided by the user. The reason this is difficult for me is because all the numbers must be accepted on only one line, I don't know how many variables the user will provide, and since I am accepting points, I must have the right amount (evens).

Here is a sample run:

Enter the points: 
``>>>4 1 3 5 1 5 9 0 2 5

My primary question is how do I unpack a random number of points? And also, how do I pair the even points together?

chopper draw lion4
  • 12,401
  • 13
  • 53
  • 100
  • 1
    as for your second question: `even, odd = points[1::2], points[0::2]`, assuming that you count element `[0]` as first (odd). – m.wasowski Mar 26 '14 at 03:42

2 Answers2

4

In Python 2:

points = map(int, raw_input().split())

In Python 3:

points = list(map(int, input().split()))

Another method - list comprehension:

points = [int(p) for p in input().split()]

To pair x and y of the points together you can use something like pairwise() on the points list: see https://stackoverflow.com/a/5389547/220700 for details.

Community
  • 1
  • 1
Sergii Dymchenko
  • 6,890
  • 1
  • 21
  • 46
1

If they are read as a string, you can use the split() method, which will return a list, then use map() to convert the items of the list to integers:

points_input = raw_input("Enter the points:")

points = map(int, points_input.split())
print points

Notes

  • The result (points) will be a list of integers.
  • If you are using Python 3.x, you have to use the method input() instead of raw_input().
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73