您好,登錄后才能下訂單哦!
1.函數input()的工作原理
message = input("Tell me something,I will repeat it back to you: ") #input()函數:讓程序停止運行,等待用戶輸入一些文本
print(message)
name = input("Please enter your name: ")
print("Hello," + name + "!")
prompt = "If you tell us who you are,we can personalize the message you see." #創建多行字符串,+=:再附加
prompt += "\nWhat is your first name?"
name_a = input(prompt)
print("Hello "+ name_a + "!")
#求模運算符
number = input("Enter a number,and I will tell you if it's even or odd: ") #使用input()
number = int(number) #用戶輸入的數值是字符串表示的,轉化為數值
if number % 2 == 0:
print("\n The number " + str(number) + " is even.")
else:
print("\nThe number " + str(number) + " is odd.")
print("\n")
2.使用while循環
num = 1
while num <=5:
print(num)
num+=1
(2)#讓用戶選擇何時退出
prompt = "\nTell me something,I will repeat it back to you."
prompt += "\nEnter 'quit' to end program. "
message = ""
while message != "quit": #message變量來提供判斷
message = input(prompt) #input()都需要一個變量存儲
if message != "quit": #打印前加個if條件再打印
print(message)
#使用標志
active = True #Ture:程序處于活躍狀態
while active:
message = input(prompt)
if message == "quit": #用if語句判斷程序狀態
active = False
else:
print(message)
#使用break退出循環
while True:
message = input(prompt)
if message == "quit":
break
else:
print(message)
(3)循環中使用continue
num = 0
while num <= 10:
num +=1**
if num % 2 == 0:
continue #滿足條件繼續循環,不滿足跳出**
print(num) **#不寫在循環內**
number = 1
while number >3:
print(number)
(4)#刪除包含特定值的所有列表元素
pet = ["dog","dog","dog","cat","rabbit"]
print(pet)
while "dog" in pet:
pet.remove("dog")
print(pet)
3.#使用用戶輸入來填充字典
responses = {} #創建空字典
active = True #循環狀態
while active:
name = input("What is your name? " ) #變量做鍵
response = input("Where are you want to go? ") #變量做值
responses[name] = response #添加鍵-值
repeat = input("Would you like to let another person respond? (yes/no) ")
if repeat == "no": #if判斷狀態
active = False
print("\n---poll Result---")
for name,response in responses.items(): #items():兩個變量分別存儲鍵-值
print(name + " would like to go " + response + ".")
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。