0

Hi, my situation is hard to explain, so I might as well explain it in depth.

I am trying to create a program where users can enter (Cartesian) coordinates of the points of a shape. Then, the program uses a vector (entered by the user) to translate the shape using its coordinates. Hopefully you understand what I'm saying. If you don't know the process/rule for translating shapes using their coordinates, you probably can't help me much because it will help if you understand what I'm trying to do.

The process starts like this:

I ask the user how many points make up their shape (what type of polygon they are translating).

Then I ask them to enter the x,y coordinates for each point. Here's the beginning code and the code for one point-saving procedure:

print('View saved points with "points".')
print()

print("Number of points:")
inputX = int(input())
if inputX < 3:
    print("Invalid.")
if inputX > 5:
    print("Invalid.")

print("Input points: x,y")
inputZero = input()
split = inputZero.split(",")
xZero,yZero = split[0],split[1]
print("("+xZero+","+yZero+") saved. Input another point.")

Now, for each point-saving section, I want the user to also, instead of entering a point's coordinates, be able to input a string like "points" and it will print all of the points saved. The problem is, I don't know how I can have integers act as coordinates for the points and have a string like "points" act like a string I can use an if statement like this (inputZero is the input for point coordinates in one of those point-saving sections):

if inputZero == "points":
    print("#All of the points previously entered")

Every response is appreciated,

Thanks

PakkuDon
  • 1,627
  • 4
  • 22
  • 21

2 Answers2

0

All that you need is a simple if/else block, and maybe a try to make sure you get valid numbers.

...

points = []
while True:
    print("Input points: x,y")
    inputZero = input()

    if inputZero == "points":
        print(previousPoints)
    else:
        try:
            split = inputZero.split(",")
            xZero,yZero = int(split[0]),int(split[1])
            print("({0}, {1}) saved. Input another point.".format(xZero, yZero))
            points.append((xZero, yZero))
        except ValueError or IndexError:
            print("Invalid input!")
Lily Mara
  • 3,859
  • 4
  • 29
  • 48
0

Not sure if I understood properly. Is this what you want?

import sys

points = []
while True:
  print("Input points: x,y")
  inputZero = raw_input()
  if inputZero == 'points':
    print "Entered points: %s" % points
  elif inputZero == 'quit':
    print "Bye!"
    sys.exit(0)
  else:
    split = inputZero.split(",")
    xZero,yZero = int(split[0]),int(split[1])
    points.append((xZero, yZero))
    print("(%s, %s) saved. Input another point." % (xZero, yZero))

Type quit to finish. Also, note I'm using raw_input instead of input to avoid eval the input (see this question and its answers)

Community
  • 1
  • 1
Savir
  • 17,568
  • 15
  • 82
  • 136