用户需要输入一组坐标,例如 (0,0), (0,1), (1,1), (1,0)
我为此编写的代码如下所示:
def get_coords():
#user_input = raw_input("Enter a list of points. For example (0,0) (0,1) (1,1) (1,0)\n")
print "Enter a list of points. For example (0,0) (0,1) (1,1) (1,0)\n"
uin = sys.stdin.readline().strip()
try:
#coords = map(int, uin.split(' '))
coords = [tuple(map(int, point.replace('(', '').replace(')', '').split(','))) for point in uin.split(' ')]
return coords
except ValueError:
print "Please enter the coordinates in the format mentioned"
exit()
我敢肯定有更好,更优雅的方式来做到这一点?