国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python -- 5. 用戶輸入和while 循環

2019-11-08 01:19:45
字體:
來源:轉載
供稿:網友

1. 函數input() 的工作原理 函數input() 讓程序暫停運行,等待用戶輸入一些文本。獲取用戶輸入后,Python將其存儲在一個變量中.

message = input("Tell me something, and I will repeat it back to you: ")PRint(message)

(1). 使用int() 來獲取數值輸入 使用函數input() 時,Python將用戶輸入解讀為字符串。

>>> age = input("How old are you? ")How old are you? 21>>> age'21'

函數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.")

(2).求模運算符 處理數值信息時,求模運算符 (%)是將兩個數相除并返回余數

number = input("Enter a number, and I'll tell you if it's 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.")

2. while 循環簡介 (1).使用while 循環

current_number = 1while current_number <= 5: print(current_number) current_number += 1

(2).使用break 退出循環 要立即退出while 循環,不再運行循環中余下的代碼,也不管條件測試的結果如何,可使用break 語句。

prompt = "/nPlease enter the name of a city you have visited:"prompt += "/n(Enter 'quit' when you are finished.) "while True: city = input(prompt) if city == 'quit': break else: print("I'd love to go to " + city.title() + "!")

(3).在循環中使用continue 要返回到循環開頭,并根據條件測試結果決定是否繼續執行循環,可使用

continue 語句current_number = 0while current_number < 10: current_number += 1 if current_number % 2 == 0: continue print(current_number)
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 陇川县| 天峨县| 江北区| 平遥县| 桃园县| 平邑县| 大宁县| 新郑市| 正安县| 吉林市| 广昌县| 灵石县| 洮南市| 德格县| 尤溪县| 原平市| 灵川县| 广灵县| 隆子县| 讷河市| 孟州市| 拉萨市| 积石山| 南乐县| 休宁县| 阿拉善右旗| 岳西县| 涟源市| 漯河市| 海淀区| 宜兰市| 东兰县| 阿鲁科尔沁旗| 九江县| 宣城市| 新平| 紫云| 油尖旺区| 高阳县| 唐山市| 昌图县|