《笨辦法學(xué)Python》筆記10-----獲取用戶輸入

程序或多或少都需要與用戶進(jìn)行交互,交互時(shí)一般按“提示-輸入-處理-輸出”流程走。

今天學(xué)習(xí)python中用于接收用戶輸入的一個(gè)built-in-function(內(nèi)建函數(shù)):raw_input()

一、代碼

#E:\lszyy\python\examples\test.py

print "How old are you?",

age = raw_input()

print "How tall are you?",

height = raw_input()

print "How much do you weigh?",

weight = raw_input()

print "So, you 're %r old, %r tall and %r heavy." % (age, height, weight)

程序在打印完“How old are you?”后,緊跟著(逗號(hào)的作用)一根短橫線閃爍,等待用戶輸入

此時(shí)可根據(jù)提示輸入年齡,而后輸入高度和體重,輸出如下:

上述代碼還有另一種寫法:

#E:\lszyy\python\examples\test2.py

age = raw_input("How old are you?")

height = raw_input("How tall are you?")

weight = raw_input("How much do you weigh?")

print "So, you 're %r old, %r tall and %r heavy." % (age, height, weight)

很顯然這種寫法更加簡(jiǎn)潔。

仔細(xì)觀察輸出結(jié)果,輸入的數(shù)字,輸出了字符串。

二、raw_input()

查看python幫助,可以使用命令行查看,也可以查看python文檔

在命令行下輸入命令:

python -m pydoc raw_input

幫助信息如下:

E:\lszyy\python\examples>python -m pydoc raw_input

Help on built-in function raw_input in module __builtin__:

(__builtin__模塊中的內(nèi)建函數(shù)raw_input的幫助信息:)

raw_input(...)

raw_input([prompt]) -> string?(返回值為string類型)

Read a string from standard input.? The trailing newline is stripped.

(從標(biāo)準(zhǔn)輸入設(shè)備讀取一個(gè)字符串。去掉換行符。)

If the user hits EOF (Unix: Ctl-D, Windows: Ctl-Z+Return), raise EOFError.

(如果用戶按下EOF符(Unix: Ctrl-D, Windows: Ctrl-Z+回車),觸發(fā)EOF異常EOFError.)

On Unix, GNU readline is used if enabled.? The prompt string, if given,is printed without a trailing newline before reading.

(Unix系統(tǒng)中,如果啟用了的話,將使用GNU readline(一個(gè)開源的跨平臺(tái)程序庫(kù),提供了交互式的文本編輯功能)。如果提供了提示字符串參數(shù)(prompt),它將在讀取之前被輸出在屏幕上。)


三、其他獲取用戶命令行輸入的函數(shù)

input()

在命令行使用幫助:

python -m pydoc input

幫助信息如下:

E:\lszyy\python\examples>python -m pydoc input

Help on built-in function input in module __builtin__:

input(...)

input([prompt]) -> value

Equivalent to eval(raw_input(prompt)).

(等于eval(raw_input(prompt)))

對(duì)代碼稍作修改,將raw_input函數(shù)修改為input函數(shù):

age = input("How old are you?")

height = input("How tall are you?")

weight = input("How much do you weigh?")

print "So, you 're %r old, %r tall and %r heavy." % (age, height, weight)

在讀取輸入時(shí),input函數(shù)將根據(jù)用戶的輸入進(jìn)行類型轉(zhuǎn)換,輸入數(shù)字轉(zhuǎn)換成數(shù)字類型,輸入字符串轉(zhuǎn)換成字符串類型,輸入字符串時(shí)需加引號(hào)。

如1:

E:\lszyy\python\examples>python test2.py

How old are you?12

How tall are you?11

How much do you weigh?11

So, you 're 12 old, 11 tall and 11 heavy.

如2:

E:\lszyy\python\examples>python test2.py

How old are you?"sf"

How tall are you?"e"

How much do you weigh?"d"

So, you 're 'sf' old, 'e' tall and 'd' heavy.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 練習(xí)11-提問(wèn) 練習(xí)程序 課后練習(xí) 練習(xí)程序 運(yùn)行結(jié)果 課后練習(xí) 其他課后練習(xí)及問(wèn)題就不看了,主要網(wǎng)上找資料對(duì)比了...
    Demoary閱讀 708評(píng)論 0 49
  • 清晨,太陽(yáng)冉冉升起掛在碧空萬(wàn)里的天上,光線照射到某間房的窗簾上,使屋里的景象呈金黃色般 床上的人歪七扭八的躺著輕聲...
    野生根號(hào)三閱讀 678評(píng)論 0 3
  • 四月清明,綿綿春雨過(guò)后,惠風(fēng)和暢。各種緬懷先烈、祭祀亡親、傳承孝道、勵(lì)志圖強(qiáng)的活動(dòng)彰顯著傳統(tǒng)節(jié)日的文化...
    隨日映象閱讀 1,513評(píng)論 0 2
  • 已經(jīng)過(guò)了二十歲的我,走在這所自己也不知道為何選擇的大學(xué)里,到處徘徊,到處尋覓。從小到大我始終是一個(gè)乖孩子,成績(jī)雖...
    設(shè)茗聽雪落閱讀 602評(píng)論 0 0

友情鏈接更多精彩內(nèi)容