pythonwhile循环love_input和while循环——Python编程从入门到实践
Python
Python開(kāi)發(fā)
Python語(yǔ)言
input和while循環(huán)——Python編程從入門(mén)到實(shí)踐
input( )
input()函數(shù):讓程序運(yùn)行暫停,等待用戶(hù)輸入。
message = input('Tell me something, and I will repeat it back to you:')print(message)
運(yùn)行結(jié)果:
Tell me something, and I will repeat it back to you: Hello Python!
Hello Python!
1. 編寫(xiě)清晰的程序
name = input("Please enter your name:")print("Hello," + name + "!")
Please enter your name: hery
Hello, hery!
提示信息超過(guò)一行時(shí):
prompt = "If you tell us who you are, we can personalize the messages you see."prompt+= 'nWhat is your name?'name=input(prompt)print("nHello," + name + "!")
2. 獲取數(shù)值的輸入
age = input("How old are you?")print(type(age))
How old are you?12
通過(guò)input()函數(shù)輸入的信息以字符串的形式存儲(chǔ),若需要將輸入作為數(shù)值使用怎么辦呢?
可以使用int()函數(shù)將其轉(zhuǎn)換為數(shù)值表示:
height = input("How tall are you, in inches?")
height=int(height)if height >= 36:print("nYou're tall enough to ride!")else:print("nYou'll be able to ride when you're a little older.")
3. 求模運(yùn)算符
求模運(yùn)算符(%):求得兩數(shù)相除返回的余數(shù)。
可用于判斷一個(gè)數(shù)是奇數(shù)還是偶數(shù):
number = input("Enter a number, and I'll tell you if it is even or odd:")
number=int(number)if number % 2 ==0:print('nThe number' + str(number) + 'is even.')else:print('nThe number' + str(number) + 'is odd.')
運(yùn)算符兩端的元素類(lèi)型要一致,故print語(yǔ)句中又需要將數(shù)值型通過(guò)str()函數(shù)轉(zhuǎn)換為字符型。
while循環(huán)
for循環(huán)是針對(duì)集合中的每個(gè)元素的一個(gè)代碼塊,而while循環(huán)是不斷的運(yùn)行,直到指定條件不滿(mǎn)足。
1. 使用while循環(huán)
current_number = 1
while current_number <= 5:print(current_number)
current_number+= 1
運(yùn)行結(jié)果:
1
2
3
4
5
2. 讓用戶(hù)選擇何時(shí)退出
prompt = "nTell me something , and I will repeat it back to you:"prompt+= "nEnter 'quit' to end the program."message= ''
while message != 'quit':
message=input(prompt)print(message)
運(yùn)行結(jié)果:
Tell me something , andI will repeat it back to you:
Enter'quit'to end the program. Hello Python
Hello Python
Tell me something ,andI will repeat it back to you:
Enter'quit' to end the program. Hello 0629Hello 0629Tell me something ,andI will repeat it back to you:
Enter'quit'to end the program. quit
quit
輸入為 quit 時(shí)循環(huán)結(jié)束。
若不想將 quit 也作為一條消息打印出來(lái),則:
prompt = "nTell me something , and I will repeat it back to you:"prompt+= "nEnter 'quit' to end the program."message= ''
while message != 'quit':
message=input(prompt)if message != 'quit':print(message)
3. 使用標(biāo)志
在要求很多條件都滿(mǎn)足的情況下才繼續(xù)運(yùn)行的程序中,可定義一個(gè)變量,用于判斷整個(gè)程序是否處于活動(dòng)狀態(tài),這個(gè)變量稱(chēng)為標(biāo)志。
prompt = "nTell me something , and I will repeat it back to you:"prompt+= "nEnter 'quit' to end the program."active=Truewhileactive:
message=input(prompt)if message == 'quit':
active=Falseelse:print(message)
敲代碼的時(shí)候把 active =False敲成了 active ='False',然后輸入quit還一直執(zhí)行循環(huán),哈哈哈
4. 使用break退出循環(huán)
prompt = "nPlease enter the name of a city you have visited:"prompt+= "n(Enter 'quit' when you are finished.)"
whileTrue:
city=input(prompt)if city == 'quit':break
else:print("I'd love to go to" + city.title() + "!")
Note: Python循環(huán)(while循環(huán)、for循環(huán))中都可使用break語(yǔ)句來(lái)推出循環(huán)。
5. 在循環(huán)中使用continue
循環(huán)中使用continue,會(huì)返回大循環(huán)開(kāi)頭,并根據(jù)條件測(cè)試結(jié)果決定是否繼續(xù)執(zhí)行循環(huán):
current_number =0while current_number < 10:
current_number+= 1
if current_number % 2 ==0:continue
else:print(current_number)
運(yùn)行結(jié)果:
1
3
5
7
9
6. 避免無(wú)限循環(huán)
x = 1
while x < 5:print(x)
x+= 1
上述的代碼塊中,若漏寫(xiě)了代碼行x += 1,這個(gè)程序?qū)](méi)完沒(méi)了地運(yùn)行。可按Ctrl + C,也可關(guān)閉顯示程序輸出的終端窗口,或關(guān)閉編輯器,結(jié)束無(wú)限循環(huán)。
內(nèi)容來(lái)源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系客服刪除
總結(jié)
以上是生活随笔為你收集整理的pythonwhile循环love_input和while循环——Python编程从入门到实践的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 去除div最后一个逗号_去除重复值、统计
- 下一篇: visual studio instal
