【问题标题】:How to enter many points in the run process?运行过程中如何输入多个点?
【发布时间】:2018-12-11 01:35:06
【问题描述】:

我现在正在解决一个问题:输入几个点的坐标,然后将它们放入三个列表中,一个用于线 y=x 上方的点,一个正好在它上面,一个在它下面。

我是新手,所以我真的很困惑。 还请告诉我如何在运行时而不是在编码时输入这些点。

【问题讨论】:

标签: python python-3.x list


【解决方案1】:

假设您有一个 (x, y) 形式的点列表,其中 x 和 y 是 int 或 float 值。假设这个列表被称为points。然后,以下将起作用:

below = []
on = []
above = []
for coordinate_pair in points:
    if coordinate_pair[0] < coordinate_pair[1]: # That is, x < y, so (x, y) is above the line y = x
        above.append(coordinate_pair)
    elif coordinate_pair[0] == coordinate_pair[1]:
        on.append(coordinate_pair)
    # I'll leave the rest to you, since I believe this is a homework assignment

如果您想在运行时输入这些点,您可以使用float(input("Enter a value for x: ")) 获取x 值,使用float(input("Enter a value for y: ")) 获取y 值。

【讨论】:

  • points 将只是您开始的列表; points 将是 (x, y) 形式的元组列表。
  • if 循环中的 0 和 1 是什么意思?
  • 这些是你的元组的索引。元组可以是 (4, 2) 的形式,其中 x 坐标为 4,y 坐标为 2。 (4, 2)[0] == 4 和 (4, 2)[1] == 2. 这是基本的元组索引。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-10-19
  • 1970-01-01
  • 2016-09-18
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
  • 2022-11-19
相关资源
最近更新 更多