1. input()函數(shù)
input()函數(shù)可以在括號中寫入提示惭适,用戶根據(jù)提示輸入信息独悴,并且將信息作為一個字符串驹沿,保存在變量中。
輸入:
prompt='If you tell us who you are, we can personalize the messages you see.'
prompt+='\nWhat is your first name? ' #prompt第二種方法
first_name=input(prompt)
last_name=input('What is your last name? ') #prompt第一種方法
print('\nhello, '+first_name+' '+last_name+'!')
輸出:
c:\py>input
If you tell us who you are, we can personalize the messages you see.
What is your first name? tao
What is your last name? ming
hello, tao ming!
2. int()函數(shù)
如果你要用戶輸入數(shù)字系草,并且要將其作為整型變量用來進行數(shù)字方面的處理,需要使用int()
函數(shù)唆涝。
輸入:
height=input('how old are you?')
height=int(height)
if height>=18:
print('you are old enogh')
else:
print('you are too young')
輸出:
c:\py>int_input
how old are you?16
you are too young
c:\py>int_input
how old are you?19
you are old enogh
3. while循環(huán)
當(dāng)while后面的值為true時悄但,執(zhí)行while下面縮進的語句。
輸入:
prompt='\nTell me something,and I will repeat it back to you:'
prompt+="\nenter 'quit' to end the program.\n"
message="" #一開始為空字符串石抡,但它依然為字符串檐嚣,這樣才能比較
while message != 'quit':
message = input(prompt)
if message != 'quit':
print(message)
輸出:
c:\py>while
Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
we
we
Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
quit
4. 用Flag控制while循環(huán)
while Falg:那我們可以通過控制Flag的True或者Flase來控制程序流。
輸入:
prompt='\nTell me something,and I will repeat it back to you:'
prompt+="\nenter 'quit' to end the program.\n"
active = True
while active:
message = input(prompt)
if message == 'quit':
active = False
else:
print(message)
輸出:
c:\py>flag
Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
else
else
Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
quit
c:\py>
5. break語句
若在循環(huán)中遇到break語句啰扛,程序跳過剩余的循環(huán)語句嚎京,直接跳出循環(huán)。
輸入:
prompt='\nTell me something,and I will repeat it back to you:'
prompt+="\nenter 'quit' to end the program.\n"
#和上一個程序比隐解,因為有了break語句鞍帝,判別是否跳出循環(huán)的任務(wù)就交給了if
while True:
message = input(prompt)
if message == 'quit':
break
else:
print(message)
輸出:
Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
me
me
Tell me something,and I will repeat it back to you:
enter 'quit' to end the program.
quit
c:\py>
6. continue語句
在循環(huán)中,continue語句會跳過剩余的循環(huán)語句煞茫,回到循環(huán)的開頭帕涌,重新開始循環(huán)摄凡。即跳過本次剩余的循環(huán),直接開始下一個循環(huán)蚓曼。
輸入:
current_number=0
while current_number <= 10:
current_number+=1
if current_number%2==0:
continue
print(current_number)
輸出:
c:\py>continue_
1
3
5
7
9
11
程序中亲澡,while循環(huán)的條件是current_number<=10,但當(dāng)current_number=10時,符合條件纫版,while又進行一次循環(huán)床绪,使current_number+1后變成11輸出,在檢測發(fā)現(xiàn)11不再<=10,所以跳出循環(huán)其弊。
7. 用while循環(huán)處理列表
(1) 將一個列表元素移到另一個列表
輸入:
unconfirmed_users=['alice','kitty','john']
confirmed_users=[]
while unconfirmed_users:
current_user=unconfirmed_users.pop()
print("verifying user: "+current_user.title())
confirmed_users.append(current_user)
print('\nthe following users are confirmed: ')
for confirmed_user in confirmed_users:
print(confirmed_user.title()) #方法一定要帶()
輸出:
c:\py>while_pop_append
verifying user: John
verifying user: Kitty
verifying user: Alice
the following users are confirmed:
John
Kitty
Alice
(2) 用while循環(huán)和.renmove()方法移除所有指定元素
.remove()方法只能移除移除第一個指定的元素癞己,加上while循環(huán),可以移除所有的指定元素梭伐。
輸入:
pets=['dog','cat','goldfish','cat','rabbit','cat']
print(pets)
while 'cat' in pets:
pets.remove('cat')
print(pets)
輸出:
c:\py>while_remove
['dog', 'cat', 'goldfish', 'cat', 'rabbit', 'cat']
['dog', 'goldfish', 'rabbit']
(3) 用while循環(huán)填充字典
輸入:
prompt_1='\nwho are you?'
prompt_2='what food do you like best?'
responses={}
while True:
name=input(prompt_1)
food=input(prompt_2)
responses[name]=food #添加鍵值對只需一條語句即可
answer=input('would you like to let another person respond?(yes/no)')
if answer=='no':
break
print('\n this is poll results\n')
print(responses)
輸出:
c:\py>while_dic
who are you?mike
what food do you like best?pear
would you like to let another person respond?(yes/no)yes
who are you?jack
what food do you like best?apple
would you like to let another person respond?(yes/no)yes
who are you?john
what food do you like best?strawberry
would you like to let another person respond?(yes/no)no
this is poll results
{'mike': 'pear', 'jack': 'apple', 'john': 'strawberry'}