繁体   English   中英

我正在尝试在要求用户输入两个输入的地方编写一行代码,但无法弄清楚如何完成此操作

[英]I'm trying to write a line of code at ask the user for two inputs but can't figure out how to complete this

为什么这行代码不能将两者分开

Lat1,long1 = input("Enter the Lat and Long of the source point separated by a comma eg 20,30").split()

默认情况下,使用split()只会在一个空间上分割。 你问用户输入一个分开的两个条目, ,所以你最终会得到一个

ValueError: not enough values to unpack (expected 2, got 1)

要解决此问题,您需要分割要与之分割的标识符。 在这种情况下,它是一个',',因此将split称为split(',')

Lat1,long1 = input("Enter the Lat and Long of the source point separated by a comma eg 20,30").split(',')

演示:

>>> Lat1,long1 = input("Enter the Lat and Long of the source point separated by a comma eg 20,30").split(',')
Enter the Lat and Long of the source point separated by a comma eg 20,3060,80
>>> Lat1
'60'
>>> long1
'80'

这是关于拆分的文档:

https://docs.python.org/3/library/stdtypes.html#str.split

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM