6.python的input函數(shù)與while循環(huán)

1. input()函數(shù)

input()函數(shù)可以在括號中寫入提示惭适,用戶根據(jù)提示輸入信息独悴,并且將信息作為一個字符串驹沿,保存在變量中。

輸入:

prompt='If you tell us who you are, we can personalize the messages you see.'
prompt+='\nWhat is your first name?  '  #prompt第二種方法

first_name=input(prompt)
last_name=input('What is your last name?  ')    #prompt第一種方法

print('\nhello, '+first_name+' '+last_name+'!')

輸出:

c:\py>input
If you tell us who you are, we can personalize the messages you see.
What is your first name?  tao
What is your last name?  ming

hello, tao ming!

2. int()函數(shù)

如果你要用戶輸入數(shù)字系草,并且要將其作為整型變量用來進行數(shù)字方面的處理,需要使用int()函數(shù)唆涝。

輸入:

height=input('how old are you?')
height=int(height)

if height>=18:
    print('you are old enogh')
else:
    print('you are too young')

輸出:

c:\py>int_input
how old are you?16
you are too young

c:\py>int_input
how old are you?19
you are old enogh

3. while循環(huán)

當(dāng)while后面的值為true時悄但,執(zhí)行while下面縮進的語句。

輸入:

prompt='\nTell me something,and I will repeat it back to you:'
prompt+="\nenter 'quit' to end the program.\n"

message=""  #一開始為空字符串石抡,但它依然為字符串檐嚣,這樣才能比較
while message != 'quit':
    message = input(prompt)
    
    if message != 'quit':
        print(message)

輸出:

c:\py>while

Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
we
we

Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
quit

4. 用Flag控制while循環(huán)

while Falg:那我們可以通過控制Flag的True或者Flase來控制程序流。

輸入:

prompt='\nTell me something,and I will repeat it back to you:'
prompt+="\nenter 'quit' to end the program.\n"

active = True

while active:
    message = input(prompt)  
    if message == 'quit':
        active = False
    else:
        print(message)

輸出:

c:\py>flag

Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
else
else

Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
quit

c:\py>

5. break語句

若在循環(huán)中遇到break語句啰扛,程序跳過剩余的循環(huán)語句嚎京,直接跳出循環(huán)。

輸入:

prompt='\nTell me something,and I will repeat it back to you:'
prompt+="\nenter 'quit' to end the program.\n"

#和上一個程序比隐解,因為有了break語句鞍帝,判別是否跳出循環(huán)的任務(wù)就交給了if
while True:     
    message = input(prompt)  
    if message == 'quit':
        break
    else:
        print(message)

輸出:

Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
me
me

Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
quit

c:\py>

6. continue語句

在循環(huán)中,continue語句會跳過剩余的循環(huán)語句煞茫,回到循環(huán)的開頭帕涌,重新開始循環(huán)摄凡。即跳過本次剩余的循環(huán),直接開始下一個循環(huán)蚓曼。

輸入:

current_number=0
while current_number <= 10:
    current_number+=1
    if current_number%2==0:
        continue
    print(current_number)

輸出:

c:\py>continue_
1
3
5
7
9
11

程序中亲澡,while循環(huán)的條件是current_number<=10,但當(dāng)current_number=10時,符合條件纫版,while又進行一次循環(huán)床绪,使current_number+1后變成11輸出,在檢測發(fā)現(xiàn)11不再<=10,所以跳出循環(huán)其弊。

7. 用while循環(huán)處理列表

(1) 將一個列表元素移到另一個列表

輸入:

unconfirmed_users=['alice','kitty','john']
confirmed_users=[]

while unconfirmed_users:
    current_user=unconfirmed_users.pop()
    
    print("verifying user: "+current_user.title())
    confirmed_users.append(current_user)

print('\nthe following users are confirmed: ')
for confirmed_user in confirmed_users:
    print(confirmed_user.title())   #方法一定要帶()

輸出:

c:\py>while_pop_append
verifying user: John
verifying user: Kitty
verifying user: Alice

the following users are confirmed:
John
Kitty
Alice

(2) 用while循環(huán)和.renmove()方法移除所有指定元素

.remove()方法只能移除移除第一個指定的元素癞己,加上while循環(huán),可以移除所有的指定元素梭伐。

輸入:

pets=['dog','cat','goldfish','cat','rabbit','cat']
print(pets)

while 'cat' in pets:
    pets.remove('cat')
    
print(pets)

輸出:

c:\py>while_remove
['dog', 'cat', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'goldfish', 'rabbit']

(3) 用while循環(huán)填充字典

輸入:

prompt_1='\nwho are you?'
prompt_2='what food do you like best?'

responses={}

while True:
    name=input(prompt_1)
    food=input(prompt_2)
    
    responses[name]=food    #添加鍵值對只需一條語句即可
    answer=input('would you like to let another person respond?(yes/no)')
    if answer=='no':
        break
print('\n this is poll results\n')
print(responses)

輸出:

c:\py>while_dic

who are you?mike
what food do you like best?pear
would you like to let another person respond?(yes/no)yes

who are you?jack
what food do you like best?apple
would you like to let another person respond?(yes/no)yes

who are you?john
what food do you like best?strawberry
would you like to let another person respond?(yes/no)no

 this is poll results

{'mike': 'pear', 'jack': 'apple', 'john': 'strawberry'}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末痹雅,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子糊识,更是在濱河造成了極大的恐慌练慕,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,470評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件技掏,死亡現(xiàn)場離奇詭異铃将,居然都是意外死亡,警方通過查閱死者的電腦和手機哑梳,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,393評論 3 392
  • 文/潘曉璐 我一進店門劲阎,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鸠真,你說我怎么就攤上這事悯仙。” “怎么了吠卷?”我有些...
    開封第一講書人閱讀 162,577評論 0 353
  • 文/不壞的土叔 我叫張陵锡垄,是天一觀的道長。 經(jīng)常有香客問我祭隔,道長货岭,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,176評論 1 292
  • 正文 為了忘掉前任疾渴,我火速辦了婚禮千贯,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘搞坝。我一直安慰自己搔谴,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,189評論 6 388
  • 文/花漫 我一把揭開白布桩撮。 她就那樣靜靜地躺著敦第,像睡著了一般峰弹。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上芜果,一...
    開封第一講書人閱讀 51,155評論 1 299
  • 那天鞠呈,我揣著相機與錄音,去河邊找鬼师幕。 笑死粟按,一個胖子當(dāng)著我的面吹牛诬滩,可吹牛的內(nèi)容都是我干的霹粥。 我是一名探鬼主播,決...
    沈念sama閱讀 40,041評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼疼鸟,長吁一口氣:“原來是場噩夢啊……” “哼后控!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起空镜,我...
    開封第一講書人閱讀 38,903評論 0 274
  • 序言:老撾萬榮一對情侶失蹤浩淘,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后吴攒,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體张抄,經(jīng)...
    沈念sama閱讀 45,319評論 1 310
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,539評論 2 332
  • 正文 我和宋清朗相戀三年洼怔,在試婚紗的時候發(fā)現(xiàn)自己被綠了署惯。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,703評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡镣隶,死狀恐怖极谊,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情安岂,我是刑警寧澤轻猖,帶...
    沈念sama閱讀 35,417評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站域那,受9級特大地震影響咙边,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜次员,卻給世界環(huán)境...
    茶點故事閱讀 41,013評論 3 325
  • 文/蒙蒙 一样眠、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧翠肘,春花似錦檐束、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,664評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽盟戏。三九已至,卻和暖如春甥桂,著一層夾襖步出監(jiān)牢的瞬間柿究,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,818評論 1 269
  • 我被黑心中介騙來泰國打工黄选, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留蝇摸,地道東北人。 一個月前我還...
    沈念sama閱讀 47,711評論 2 368
  • 正文 我出身青樓办陷,卻偏偏與公主長得像貌夕,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子民镜,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,601評論 2 353

推薦閱讀更多精彩內(nèi)容

  • 概要 64學(xué)時 3.5學(xué)分 章節(jié)安排 電子商務(wù)網(wǎng)站概況 HTML5+CSS3 JavaScript Node 電子...
    阿啊阿吖丁閱讀 9,182評論 0 3
  • 奔襲了好幾百里 穿過黎明 初升的半月和漸落的星辰 穿過 我一直懼怕的黑暗 終于在科技制造的光明里 與她 我生命...
    阿蓮0609閱讀 194評論 0 3
  • 名人自殺制圈,在這個時代已不是什么新鮮事了们童,但是,每一次聽到還是感到有點兒震驚鲸鹦。 其實吧慧库,震驚的是自殺背后的那種根深蒂...
    枯葉蕭瑟閱讀 962評論 10 29
  • 今天和婭琪團隊的家人一起到福利院慰問孤寡老人,送上我們得愛心霜馋嗜,能夠幫助到她們齐板,心里滿滿的幸福,最美莫過于心美嵌戈,愛...
    宋東輝完顏古方閱讀 200評論 0 3
  • 立春弄花 文/洪燕 今日立春陽光像孔雀一樣開屏 我把花草一一搬于陽臺日照 花草說 冬季已熬過 俺將無比...
    __洪燕閱讀 149評論 0 2