鐢ㄦ埛杈撳叆鍜寃hile寰幆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了鐢ㄦ埛杈撳叆鍜寃hile寰幆相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/verify' title='verify'>verify   str   鍒╃敤   rom   rem   eth   response   鍒楄〃   person   

input()

prompt = "If you tell us who you are, we can personalize the messages you see."
prompt += "
what is your first name? "

name = input(prompt)
print("
hello, " + name + "!")

杈撳嚭锛?If you tell us who you are, we can personalize the messages you see.
what is your first name? Eric

hello, Eric!

姹傛ā杩愮畻绗︼細%锛屽畠灏嗕袱涓暟鐩搁櫎骞惰繑鍥炰綑鏁?/p>

print(4%3)
print(6%3)
print(1%3)
print(-1%3)  #涓哄暐鏄?
杈撳嚭锛?
1
0
1
2

浣欐暟濡傛灉鏄?锛屽彲鍒╃敤杩欑偣鏉ュ垽鏂/鍋舵暟

number = input("Enter a number, and I鈥榣l tell you if it鈥榮 even or odd: ")
number = int(number)
if number % 2 == 0:   #numbber % 2 == 0 ,number灏辨槸鍋舵暟锛屽惁鍒欐槸濂囨暟
    print("
The number " + str(number) + " is even.")
else:
    print("
The number " + str(number) + " is odd.")
杈撳嚭锛?Enter a number, and I鈥榣l tell you if it鈥榮 even or odd: 5

The number 5 is odd.

while寰幆锛氫笉鏂湴杩愯锛岀煡閬撴寚瀹氱殑鏉′欢涓嶆弧瓒充负姝€?/p>

prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter 鈥榪uit鈥?to end the program."

message = ""
while message !=鈥?/span>quit鈥?/span>:
    message = input(prompt)
    
    if message != 鈥?/span>quit鈥?/span>:
        print(message)
杈撳嚭锛?Tell me something, and I will repeat it back to you:
Enter 鈥?/span>quit鈥?/span> to end the program.hello everyone!
hello everyone!

Tell me something, and I will repeat it back to you:
Enter 鈥?span style="color: #800000">quit鈥?span style="color: #000000"> to end the program.hello again!
hello again!
Tell me something, and I will repeat it back to you: 
Enter
鈥?/span>quit鈥?/span> to end the program.quit

鏍囪

prompt = "
Tell me something, and I will repeat it back to you:"
prompt += "
Enter 鈥榪uit鈥?to end the program."

active = True
while active:
    message = input(prompt)
    if message == 鈥?/span>quit鈥?/span>:
        active = False
    else:
        print(message)

break

prompt = "
PLS enter the name of a city you have visited:"
prompt += "
(Enter 鈥榪uit鈥?when you are finished.) "

while True:
    city = input(prompt)
    
    if city == 鈥?/span>quit鈥?/span>:
        break  #閫€鍑哄惊鐜?/span>
    else:
        print("I鈥榙 love to go to " + city.title() + "!")
杈撳嚭锛?
PLS enter the name of a city you have visited:
(Enter 鈥?/span>quit鈥?/span> when you are finished.) Beijing
I鈥?/span>d love to go to Beijing!

PLS enter the name of a city you have visited:
(Enter 鈥?/span>quit鈥?/span> when you are finished.) ShenYang
I鈥?/span>d love to go to Shenyang!

PLS enter the name of a city you have visited:
(Enter 鈥?/span>quit鈥?/span> when you are finished.) quit

continue

current_number = 0

while current_number < 10:
    current_number += 1
    if current_number % 2 == 0:
        continue
    
    print(current_number)
        
杈撳嚭锛?1
3
5
7
9

娉細濡傛灉鍛戒护琛屽嚭鐜版棤闄愬惊鐜紝Ctrl+C閫€鍑?/p>

楠岃瘉鏂版敞鍐岀敤鎴凤紝灏嗘湭楠岃瘉鐢ㄦ埛绉诲姩鍒板凡楠岃瘉鐢ㄦ埛鍒楄〃涓?/p>

unconfirmed_users = [鈥?/span>alice鈥?/span>,鈥?/span>brian鈥?/span>,鈥?/span>candace鈥?/span>]  #寰呴獙璇佺敤鎴?confirmed_users = []    #宸查獙璇佺殑鐢ㄦ埛

while unconfirmed_users:   #楠岃瘉姣忎釜鐢ㄦ埛
    current_user = unconfirmed_users.pop()
    
    print("Verifying user: " + current_user.title())
    confirmed_users.append(current_user)
#鏄剧ず鎵€鏈夊凡楠岃瘉鐢ㄦ埛
print("
The following users have been confirmed: ")
for confirmed_user in confirmed_users:
    print(confirmed_user.title())
    
杈撳嚭锛?Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice

鍒犻櫎鍒楄〃涓墍鏈夌殑鎸囧畾鍏冪礌

pets = [鈥?/span>dog鈥?/span>,鈥?/span>cat鈥?/span>,鈥?/span>dog鈥?/span>,鈥?/span>goldfish鈥?/span>,鈥?/span>cat鈥?/span>,鈥?/span>rabbit鈥?/span>,鈥?/span>cat鈥?/span>]
print(pets)

while 鈥?/span>cat鈥?/span> in pets:
    pets.remove(鈥?/span>cat鈥?/span>)

print(pets)
杈撳嚭锛?[鈥?/span>dog鈥?/span>, 鈥?/span>cat鈥?/span>, 鈥?/span>dog鈥?/span>, 鈥?/span>goldfish鈥?/span>, 鈥?/span>cat鈥?/span>, 鈥?/span>rabbit鈥?/span>, 鈥?/span>cat鈥?/span>]
[鈥?/span>dog鈥?/span>, 鈥?/span>dog鈥?/span>, 鈥?/span>goldfish鈥?/span>, 鈥?/span>rabbit鈥?/span>]

 

responses = {}

active = True

while active:
    name = input("what鈥榮 your name? ")
    response = input("what鈥榮 your favorite language:")
    
    responses[name] = response
    
    repeat = input("go on?(yes/no)")
    if repeat == 鈥?/span>no鈥?/span>:
        active = False
        
print("
 -- Result -- ")
for name,response in responses.items():
    print(name.title() + "鈥榮 favorite language is :" + response.title())
杈撳嚭锛?what鈥?/span>s your name? alex
what鈥?/span>s your favorite language:python
go on?(yes/no)yes
what鈥?/span>s your name? suki
what鈥?/span>s your favorite language:C
go on?(yes/no)no

 -- Result --
Alex鈥?/span>s favorite language is :Python
Suki鈥?/span>s favorite language is :C

 











以上是关于鐢ㄦ埛杈撳叆鍜寃hile寰幆的主要内容,如果未能解决你的问题,请参考以下文章

java涓璮or寰幆鍜寃hile寰幆锛屽摢涓洿蹇紵

Go鑾峰彇鐢ㄦ埛缁堢杈撳叆

鎴戜笌涓栫晫鏍兼牸涓嶅叆,鎴戜笌python鎯烘兒鐩告儨

linux鍩虹11-bash缂栫▼(瀛楃涓叉祴璇?鍜?for寰幆)

5. 绗?5 绔?寰幆

php涓殑foreach寰幆