簡體   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