python基礎(六)
阿新 • • 發佈:2020-09-07
#函式input(),程式暫停執行,等待使用者輸入一些文字,獲取輸入後,將其儲存在一個變數中 message = input("tell me") print(message)
tell mehi hi
name = input("Please enter your name:") print("Hello, " + name + "!")
Please enter your name:cui Hello, cui!
prompt = "hihi" prompt = "\nwhat is your name" name = input(prompt) print("\nHello," + name + "!")
what is your namecui Hello, cui!
#使用int()來獲取數值輸入,將使用者輸入解讀為字串,無法與數值進行比較 age = input("How old are you?")
How old are you?22
age
'22'
age >= 18
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-6-4ce6028355cc> in <module>
----> 1 age >= 18
TypeError: '>=' not supported between instances of 'str' and 'int'
age = int(age)
age >= 18
True
height = input("How tall are you, in inches") height = int(height)
How tall are you, in inches71
if height >= 36: print("\nYou are tall enough to ride!") else: print("\nYou'll be able to ride")
You are tall enough to ride!
#求模運算子 4%3
#用於判斷奇數偶數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.")
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)
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hihi hihi Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.露露 露露 Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.xixi xixi Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.quit
#用於判斷奇數偶數 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.")
enter a number, and I'll tell you if it's even or odd:51 The number 51is odd.
#while迴圈 current_number = 1 while current_number <= 5: print(current_number) current_number += 1
1 2 3 4 5
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, and I will repeat it back to you: Enter 'quit' to end the program.hi hi Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hello hello Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.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)
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hi Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hello Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.quit
#使用標誌 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)
Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hi hi Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.hello hello Tell me something, and I will repeat it back to you: Enter 'quit' to end the program.quit
#使用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("mmmmmm" + city.title() + "!")
Please enter the name of a city you have visited. (enter 'quit' when you are finished.)nanjing mmmmmmNanjing! Please enter the name of a city you have visited. (enter 'quit' when you are finished.)guangdong mmmmmmGuangdong! Please enter the name of a city you have visited. (enter 'quit' when you are finished.)quit
Please enter the name of a city you have visited. (enter 'quit' when you are finished.)nanjing mmmmmmNanjing! Please enter the name of a city you have visited. (enter 'quit' when you are finished.)guangdong mmmmmmGuangdong! Please enter the name of a city you have visited. (enter 'quit' when you are finished.)quit
#continue number = 0 while number < 10: number += 1 if number % 2 == 0: continue print(number)
1 3 5 7 9
#避免無限迴圈 #處理列表和字典 unconfirmed_users = ['baba','mama','jing'] confirmed_users = [] while unconfirmed_users: current_user = unconfirmed_users.pop() print("vertify:" + current_user.title()) confirmed_users.append(current_user)
vertify:Jing
vertify:Mama
vertify:Baba
print("new:") for confirmed_user in confirmed_users: print(confirmed_user.title())
new: Jing Mama Baba
#刪除特定值的所有列表元素 pet = ['dog','cat','dog','goldfish','cat','rabbit','cat'] print(pet)
['dog', 'cat', 'dog', 'goldfish', 'cat', 'rabbit', 'cat']
pets = ['dog','cat','dog','goldfish','cat','rabbit','cat'] while 'cat' in pets: pets.remove('cat') print(pets)
['dog', 'dog', 'goldfish', 'rabbit']
#使用使用者輸入來填充字典 responses = {} #設定一個標誌,指出調查是否繼續 polling_active = True while polling_active: #提示輸入被調查者的名字和回答 name = input("\nWhat is your name?") response = input("Which mountain would you like to climb someday?") #將答卷儲存在字典中 responses[name] = response #看看是否還有人要參與調查 repeat = input("Would you like to let another person respond?(yes/no)") if repeat == 'no': polling_active = False
What is your name?cui Which mountain would you like to climb someday?huang Would you like to let another person respond?(yes/no)yes What is your name?lu Which mountain would you like to climb someday?zijin Would you like to let another person respond?(yes/no)yes What is your name?ting Which mountain would you like to climb someday?tai Would you like to let another person respond?(yes/no)no
#調查結束,顯示結果 print("\n--- Poll Results ---") for name,response in responses.items(): print(name + " would like to climb " + response + ".")
--- Poll Results --- cui would like to climb huang. lu would like to climb zijin. ting would like to climb tai.