试图从用户输入中获得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()发布于 2020-11-13 22:19:14
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()发布于 2020-11-13 22:19:25
将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()使用空格字符作为分隔符)
还修复了您的输入行,其中缺少一个结束括号")“。
发布于 2020-11-13 22:22:53
从您的代码看,您似乎正在尝试从输入构建一个列表,因此在开始时将x和y设置为列表。但是,这些列表将被input语句覆盖,实际上您最终得到的是字符串。当在字符串上迭代时,一次只产生一个字符。
有一种轻量级的方法,可以通过在输入后将字符串转换为列表,然后在空格上拆分每个项目来实现您想要的结果:
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()https://stackoverflow.com/questions/64822419
复制相似问题