1. 程式人生 > 其它 >Python學習筆記:input接受輸入

Python學習筆記:input接受輸入

一、介紹

Pythoninput() 函式接受一個標準輸入資料,返回為字元型別。

無論是 intfloatlist 等,在輸入的時候,都是以字串儲存。

使用語法:

a = input([prompt])  # 提示資訊 等待使用者輸入

二、實操

1.接受一個值

a = input('input:') # input:100
type(a) # str
b = input() # abc
type(b) # str

2.接受多個值

# 輸入
a, b, c = (input('請輸入三角形的邊長:').split()) # 5 7 8
a = int(a)
b = int(b)
c = int(c)
# 計算半周長
p = (a + b + c) / 2
# 計算面積
s = (p*(p-a)*(p-b)*(p-c)) ** 0.5
# 輸出
print('三角形面積為:', format(s, '.2f'))
# 三角形面積為: 17.32

3.接受多行輸入

最近考試,在測試環境中除錯的時候,接受輸入樣例是以多行輸入,則可以按下面方式接受。

a = input()
b = input()

另外,需要注意輸入 \n\t 換行符會被轉義的問題。

參考連結:Python3 input() 函式

參考連結:【Python】input()函式用法小結