pythonwhile循环love_input和while循环——Python编程从入门到实践
Python
Python開發
Python語言
input和while循環——Python編程從入門到實踐
input( )
input()函數:讓程序運行暫停,等待用戶輸入。
message = input('Tell me something, and I will repeat it back to you:')print(message)
運行結果:
Tell me something, and I will repeat it back to you: Hello Python!
Hello Python!
1. 編寫清晰的程序
name = input("Please enter your name:")print("Hello," + name + "!")
Please enter your name: hery
Hello, hery!
提示信息超過一行時:
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. 獲取數值的輸入
age = input("How old are you?")print(type(age))
How old are you?12
通過input()函數輸入的信息以字符串的形式存儲,若需要將輸入作為數值使用怎么辦呢?
可以使用int()函數將其轉換為數值表示:
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. 求模運算符
求模運算符(%):求得兩數相除返回的余數。
可用于判斷一個數是奇數還是偶數:
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.')
運算符兩端的元素類型要一致,故print語句中又需要將數值型通過str()函數轉換為字符型。
while循環
for循環是針對集合中的每個元素的一個代碼塊,而while循環是不斷的運行,直到指定條件不滿足。
1. 使用while循環
current_number = 1
while current_number <= 5:print(current_number)
current_number+= 1
運行結果:
1
2
3
4
5
2. 讓用戶選擇何時退出
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)
運行結果:
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 時循環結束。
若不想將 quit 也作為一條消息打印出來,則:
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. 使用標志
在要求很多條件都滿足的情況下才繼續運行的程序中,可定義一個變量,用于判斷整個程序是否處于活動狀態,這個變量稱為標志。
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)
敲代碼的時候把 active =False敲成了 active ='False',然后輸入quit還一直執行循環,哈哈哈
4. 使用break退出循環
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循環(while循環、for循環)中都可使用break語句來推出循環。
5. 在循環中使用continue
循環中使用continue,會返回大循環開頭,并根據條件測試結果決定是否繼續執行循環:
current_number =0while current_number < 10:
current_number+= 1
if current_number % 2 ==0:continue
else:print(current_number)
運行結果:
1
3
5
7
9
6. 避免無限循環
x = 1
while x < 5:print(x)
x+= 1
上述的代碼塊中,若漏寫了代碼行x += 1,這個程序將沒完沒了地運行。可按Ctrl + C,也可關閉顯示程序輸出的終端窗口,或關閉編輯器,結束無限循環。
內容來源于網絡,如有侵權請聯系客服刪除
總結
以上是生活随笔為你收集整理的pythonwhile循环love_input和while循环——Python编程从入门到实践的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 去除div最后一个逗号_去除重复值、统计
- 下一篇: python删除列表中字符串_pytho