亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試將輸入轉換為列表

嘗試將輸入轉換為列表

烙印99 2023-12-29 10:32:11
我正在嘗試從用戶那里獲取 2 個點的輸入并輸出距離。我陷入了將輸入轉換為輸出列表的困境。我可能以錯誤的方式思考它,任何幫助我走向正確方向的幫助都是值得贊賞的。import mathp1 = input("please enter x1 and y1: ")p2 = input("please enter x2 and y2: ")x1y1 = p1.split(',')x2y2 = p2.split(',')distance = math.sqrt( ((x1y1[0]-x2y2[0])**2)+((x1y1[1]-x2y2[1])**2) )print(distance)
查看完整描述

5 回答

?
鴻蒙傳說

TA貢獻1865條經驗 獲得超7個贊

您可以使用列表理解將輸入轉換為ints,然后進行解構賦值以將它們分配給兩個不同的變量:


from math import sqrt


[x1, y1] = [int(n) for n in input("please enter x1 and y1: ").split()]

[x2, y2] = [int(n) for n in input("please enter x2 and y2: ").split()]


print(f"Distance: {sqrt((x1-x2)**2+(y1-y2)**2)}")


查看完整回答
反對 回復 2023-12-29
?
偶然的你

TA貢獻1841條經驗 獲得超3個贊

首先將每個元素轉換為 int:


p1 = input("please enter x1 and y1: ")

p2 = input("please enter x2 and y2: ")


x1y1 = [int(x) for x in p1.split(',')]

x2y2 = [int(y) for y in p2.split(',')]


查看完整回答
反對 回復 2023-12-29
?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

這可以幫助:


from math import sqrt

xi, yi = [int(i) for i in input().split()]

xf, yf = [int(i) for i in input().split()]

print(math.sqrt((xf-xi)**2 + (yf-yi)**2))


查看完整回答
反對 回復 2023-12-29
?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

import math

p1 = input("please enter x1 and y1: ")

p2 = input("please enter x2 and y2: ")


x1y1 = [int(num) for num in p1.split(',')]

x2y2 = [int(num) for num in p2.split(',')]

distance = math.sqrt( ((x1y1[0]-x2y2[0])**2)+((x1y1[1]-x2y2[1])**2) )


print(distance)

Str 應轉換為 int。


查看完整回答
反對 回復 2023-12-29
?
心有法竹

TA貢獻1866條經驗 獲得超5個贊

您可以使用map()輕松轉換為int


import math

p1 = input("please enter x1 and y1: ")

p2 = input("please enter x2 and y2: ")


x1y1 = list(map(int, p1.split(',')))

x2y2 = list(map(int, p2.split(',')))


distance = math.sqrt( ((x1y1[0]-x2y2[0])**2)+((x1y1[1]-x2y2[1])**2) )


print(distance)


查看完整回答
反對 回復 2023-12-29
  • 5 回答
  • 0 關注
  • 248 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號