打印/写入 x 和 y 坐标时,数字 9 >不匹配



试图从用户输入中获得一个简单的x,y坐标列表,但超过9的数字基本上跳过了一对。最后看起来是这样的:

1 6

4 9

1

2 7---而不是12 7

x = []
y = []
x = input("Write chosen x-coordinate(s) here (with space between each number): ")
y = input("Write chosen y-coordinate(s) here (with space between each number): ")

def datalist():
global x, y
for x, y in zip(x, y):
print(x, y)
datalist()
x = input("Write chosen x-coordinate(s) here (with space between each number): ")
y = input("Write chosen y-coordinate(s) here (with space between each number): ")
print(type(x))
print(type(y))
# x and y are strings as input function returns string so you need to split the string to list
x=x.split()
y=y.split()
def datalist():
global x, y
for i, j in zip(x, y):
print(i, j)
datalist()

将x和y定义为字符串,并在用户输入后将其制成列表,如:

x = input("Write chosen x-coordinate(s) here (with space between each number): ")
y = input("Write chosen y-coordinate(s) here (with space between each number): ")

x = x.split()
y = y.split()

def datalist():
global x, y
for x, y in zip(x, y):
print(x, y)
datalist()

基本上,您要做的是将x和y设置为字符串而不是数组。

然后,您将使用split((函数将它们制成列表,并使用空格作为分隔符。(不带参数的split((使用空格字符作为分隔符(

还修复了缺少右括号的输入行("(&";。

从您的代码中,看起来您正试图从输入构建一个列表,因此在一开始就将x和y设置为列表。但是,这些列表随后被输入语句覆盖,实际上最终得到的是字符串。当对字符串进行迭代时,一次只生成一个字符。

有一种简单的方法,可以通过在输入后将字符串转换为列表,然后在空间上拆分每个项目来实现您想要的内容:

x = input("Write chosen x-coordinate(s) here (with space between each number): "
y = input("Write chosen y-coordinate(s) here (with space between each number): "

def datalist():
global x, y

x = x.split(' ')
y = y.split(' ')
for x, y in zip(x, y):
print(x, y)
datalist()

当您以这种方式接收输入以及x&y将是字符串。

x = input("Write chosen x-coordinate(s) here (with space between each number):").split()
y = input("Write chosen y-coordinate(s) here (with space between each number):").split()

def datalist():
global x, y
for x, y in zip(x, y):
print(x, y)
datalist()

最新更新