Python 是最容易學習和使用的語言之一,同時又非常強大。 Python 是一個通用的解釋型、交互式、面向對象和高級
編程語言。
蟒蛇 input()
和 raw_input()
函數用於從標準輸入(例如鍵盤)讀取數據。 這篇文章我將展示如何在 python 2/python 3 版本上使用 python raw_input 函數和示例。
從 Python 2 的鍵盤讀取輸入
Python 2 有兩個版本的輸入函數, input()
和 raw_input()
.
這 input()
如果接收到的數據包含在引號 ” 或 “” 中,則函數將接收到的數據視為字符串,否則
數據被視為數字。
In Python 2
>>> age = input('How old are you ?')
How old are you ?: 30 # entered data is treated as number.
>>> print age
30
>>> age = input('How old are you ? :')
How old are you ? : '30' # entered data is treated as string.
>>> print age
'30'
這 input('How old are you ?')
函數詢問您的年齡,您將從鍵盤輸入整數值 30,該值將存儲在 age
變量為整數。 當我們打印 age
變量與 print age
, 輸出是整數值 30。
另一方面, input('How old are you ?')
函數詢問您的年齡,您將從鍵盤輸入字符串“30”值,該值將存儲在 age
變量作為字符串。 當我們打印 age
變量與 print age
函數,輸出是字符串值’30’。
這 raw_input()
即使沒有引號 ” 或 “”,函數也將接收到的數據視為字符串。
In Python 2
>>> age = raw_input("How old are you ? :")
How old are you ? : 30 # entered data is treated as string even without ''
>>> print age
'30'
>>> age = raw_input("How old are you ? :")
How old are you ? : '30' # entered data treated as string including ''
>>> print age
"'30'"
這 raw_input('How old are you ?')
函數詢問您的年齡,您將從鍵盤輸入整數值 30 或字符串值“30”,該值將存儲在 age
變量作為字符串。 當我們打印 age
變量與 print age
函數,輸出是字符串值“30”或“30”。
從 Python 3 的鍵盤讀取輸入
在 Python 3 中, raw_input()
功能已棄用。 此外,接收到的數據總是
視為字符串。
In Python 3
>>> age = input("How old are you ? :")
How old are you ? : 30
>>> print(age)
'30'
>>> age = input("How old are you ? :")
How old are you ? : '30' # entered data treated as string with or without ''
>>> print(age)
"'30'"
>>> age = raw_input("How old are you ? :") # will result NameError
Traceback (most recent call last):
File "<input>", line 1, in <module>
NameError: name 'raw_input' is not defined
這 input('How old are you ?')
函數詢問您的年齡,您將從鍵盤輸入整數值 30 或字符串值“30”,該值將存儲在 age
變量作為字符串。 當我們打印 age
變量與 print(age)
函數,輸出是字符串值“30”或“30”。
如何將字符串值轉換為整數值
要將字符串值轉換為整數值,我們將使用 int()
功能。
In Python 2
>>> age = int(raw_input("How old are you ? :"))
How old are you ? : 30
>>> print(age)
30
>>> age = int(raw_input("How old are you ? :"))
How old are you ? : '30'
>>> print(age)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 30: "'30'"
In Python 3
>>> age = int(input("How old are you ? :"))
How old are you ? : 30
>>> print(age)
30
>>> age = int(input("How old are you ? :"))
How old are you ? : '30'
>>> print(age)
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 30: "'30'"
當我們輸入數字值時, raw_input()
python 2中的函數和 input()
在python 3中將其存儲為字符串值。 在那之後 int()
函數將其轉換為整數值。 如果我們在引號之間輸入數字值
如何計算圓的面積
我們將製作一個簡單的程序來讀取鍵盤輸入以及如何在 Ubuntu 18.04 上執行它,計算圓面積。
In Python 2
#!/usr/bin/python
import math
radius = int(raw_input("Please enter the radius of the circle : "))
area_of_circle = math.pi * radius**2
print area_of_circle
我們將創建文件 circle_area2.py
並將上面的代碼存儲在其中。
smart@li615-141:~$ sudo nano circle_area2.py
要執行此程序,我們將鍵入以下命令。
smart@li615-141:~$ sudo python circle_area2.py
In Python 3
#!/usr/bin/python3
import math
radius = int(input("Please enter the radius of the circle : "))
area_of_circle = math.pi * radius**2
print(area_of_circle)
我們將創建文件 circle_area3.py
並將上面的代碼存儲在其中。
smart@li615-141:~$ sudo nano circle_area3.py
要執行此程序,我們將鍵入以下命令。
smart@li615-141:~$ sudo python circle_area3.py
另請閱讀:
- 如何在 Ubuntu 18.04 上設置 Python 虛擬環境
- 如何在 Ubuntu 18.04 上使用 Python 3 安裝 Flask
- 如何在 Ubuntu 18.04 上安裝最新的 Python
raw_input()
和 input()
python 2和python 3中的函數對於通過鍵盤接收用戶的數據非常重要。 我們也必須使用 int()
函數將輸入字符串值轉換為整數值。