python基礎(chǔ)5-用戶輸入與while循環(huán)

    獲得用戶輸入是,使用`input()`函數(shù)。此外,還要使用while循環(huán)讓程序不斷運(yùn)行,直到指定的條件不滿足為止。

5.1、函數(shù)input()工作原理

input()讓程序暫停運(yùn)行,等待用戶輸入一些文本,獲得用戶輸入后,將其存儲(chǔ)在一個(gè)變量中,以方便程序使用。

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

5.1.1、使用int()來(lái)獲取數(shù)值輸入

使用input()輸入時(shí),python將用戶輸入保存為字符串。

可以使用int()函數(shù)將input()獲得的值轉(zhuǎn)化為int類型

age = input("How old are you: ")
age = int(age)

5.1.2、求模運(yùn)算符

求模運(yùn)算符%將兩個(gè)數(shù)相除并返回余數(shù)

4%3 = 1
5%3 = 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) + " si odd.")

5.2、while循環(huán)

for循環(huán)針對(duì)集合中的每個(gè)元素,而while循環(huán)不斷地運(yùn)行,知道指定的條件不滿足為止

5.2.1、使用while循環(huán)

你可以使用while循環(huán)來(lái)數(shù)數(shù)

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

5.2.2、讓用戶選擇何時(shí)退出

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

message = ""
while message != 'quit':
  message = input(prompt)
  print(message)

5.2.3、使用標(biāo)志

定義一個(gè)變量,用于判斷整個(gè)程序是否處于活動(dòng)狀態(tài),這個(gè)變量被稱為標(biāo)志。

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

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

5.2.4、使用break退出循環(huán)

要立即退出while循環(huán),不在運(yùn)行循環(huán)中余下的代碼,可以使用break語(yǔ)句。

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

while True:
    message = input(prompt)

    if message == 'quit':
        break
    else:
        print(message)

5.2.5、在循環(huán)中使用continue

要返回到循環(huán)開(kāi)頭,并根據(jù)條件測(cè)試結(jié)果決定是否繼續(xù)執(zhí)行循環(huán),可使用continue語(yǔ)句。

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

5.3、使用while循環(huán)來(lái)處理列表和字典

5.3.1、在列表之間移動(dòng)元素

userconfirmed_users = ['alice', 'brian', 'candace']
confirmed_users = []

while unconfirmed_users:
  current_user = unconfirmed_users.pop()
  print("Verifying user: " + current_user.title())
  
  cofirmed_users.append(current_user)

5.3.2、刪除列表中重復(fù)元素

刪除列表中特定元素使用remove()函數(shù),但是函數(shù)只能刪除一個(gè)特定值,可以配合while遍歷整個(gè)列表,將所有特定值全部刪除

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

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

5.3.3、使用用戶輸入來(lái)填充字典

可使用while提示用戶輸入任意數(shù)量的信息。

response = {}

poll_active = True

while poll_active:
  name = input("\nWhat is your name?")
  response = input("which mountain sould you like to climb someday?")
  
  response[name] = response
  
  repeat = input("would you like to let another person respond?(yes/no)")
  if repeat = 'no':
    polling_active = False
    
print("\n--poll results--")
for name, response in responses.items():
  print(name + " would like to climb " + response + ".")
最后編輯于
?著作權(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ù)。

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