python如何输入一个数停止输出可循环部分_Python 第04周:控制与循环
if語句 if語句用來檢驗一個條件, 如果 條件為真,我們運行一塊語句(稱為 if-塊 ), 否則 我們處理另外一塊語句(稱為 else-塊 )。 else 從句是可選的。
練習: 使用if語句 number = 23
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.' # New block starts here
print "(but you do not win any prizes!)" # New block ends here
elif guess < number:
print 'No, it is a little higher than that' # Another block
# You can do whatever you want in a block ...
else:
print 'No, it is a little lower than that'
# you must have guess > number to reach here
print 'Done'
練習輸出: Enter an integer : 50
No, it is a little lower than that
Done
Enter an integer : 22
No, it is a little higher than that
Done
Enter an integer : 23
Congratulations, you guessed it.
(but you do not win any prizes!)
Done
while語句 只要在一個條件為真的情況下,while語句允許你重復執行一塊語句。while語句是所謂 循環 語句的一個例子。while語句有一個可選的else從句。
練習: 使用while語句 number = 23
running = True
while running:
guess = int(raw_input('Enter an integer : '))
if guess == number:
print 'Congratulations, you guessed it.'
running = False # this causes the while loop to stop
elif guess < number:
print 'No, it is a little higher than that'
else:
print 'No, it is a little lower than that'
else:
print 'The while loop is over.'
# Do anything else you want to do here
練習輸出 Enter an integer : 50
No, it is a little lower than that.
Enter an integer : 22
No, it is a little higher than that.
Enter an integer : 23
Congratulations, you guessed it.
The while loop is over.
Done
練習程序是如何工作的?
在這個程序中,我們仍然使用了猜數游戲作為例子,但是這個例子的優勢在于用戶可以不斷的猜數,直到他猜對為止——這樣就不需要像前面那個例子那樣為每次猜測重復執行一遍程序。這個例子恰當地說明了while語句的使用。 我們把raw_input和if語句移到了while循環內,并且在while循環開始前把running變量設置為True。首先,我們檢驗變量running是否為True,然后執行后面的 while-塊 。在執行了這塊程序之后,再次檢驗條件,在這個例子中,條件是running變量。如果它是真的,我們再次執行while-塊,否則,我們繼續執行可選的else-塊,并接著執行下一個語句。
當while循環條件變為False的時候,else塊才被執行——這甚至也可能是在條件第一次被檢驗的時候。如果while循環有一個else從句,它將始終被執行,除非你的while循環將永遠循環下去不會結束!
True和False被稱為布爾類型。你可以分別把它們等效地理解為值1和0。在檢驗重要條件的時候,布爾類型十分重要,它們并不是真實的值1。
else塊事實上是多余的,因為你可以把其中的語句放在同一塊(與while相同)中,跟在while語句之后,這樣可以取得相同的效果。
for循環 for..in是另外一個循環語句,它在一序列的對象上 遞歸 即逐一使用隊列中的每個項目。我們會在后面的章節中更加詳細地學習序列。
練習: 使用for語句 for i in range(1, 5):
print i
else:
print 'The for loop is over'
練習輸出: 1
2
3
4
The for loop is over
練習程序是如何工作的? 在這個程序中,我們打印了一個 序列 的數。我們使用內建的range函數生成這個數的序列。
我們所做的只是提供兩個數,range返回一個序列的數。這個序列從第一個數開始到第二個數為止。例如,range(1,5)給出序列[1, 2, 3, 4]。默認地,range的步長為1。如果我們為range提供第三個數,那么它將成為步長。例如,range(1,5,2)給出[1,3]。記住,range 向上 延伸到第二個數,即它不包含第二個數。
for循環在這個范圍內遞歸——for i in range(1,5)等價于for i in [1, 2, 3, 4],這就如同把序列中的每個數(或對象)賦值給i,一次一個,然后以每個i的值執行這個程序塊。在這個例子中,我們只是打印i的值。
記住,else部分是可選的。如果包含else,它總是在for循環結束后執行一次,除非遇到break語句。
記住,for..in循環對于任何序列都適用。這里我們使用的是一個由內建range函數生成的數的列表,但是廣義說來我們可以使用任何種類的由任何對象組成的序列!我們會在后面的章節中詳細探索這個觀點。
break語句 break語句是用來 終止 循環語句的,即哪怕循環條件沒有稱為False或序列還沒有被完全遞歸,也停止執行循環語句。
一個重要的注釋是,如果你從for或while循環中 終止 ,任何對應的循環else塊將不執行。
練習: 使用break語句 while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
print 'Length of the string is', len(s)
print 'Done'
練習輸出: Enter something : Programming is fun
Length of the string is 18
Enter something : When the work is done
Length of the string is 21
Enter something : if you wanna make your work also fun:
Length of the string is 37
Enter something :?????? use Python!
Length of the string is 12
Enter something : quit
Done
練習程序是如何工作的? 在這個程序中,我們反復地取得用戶地輸入,然后打印每次輸入地長度。我們提供了一個特別的條件來停止程序,即檢驗用戶的輸入是否是'quit'。通過 終止 循環到達程序結尾來停止程序。
輸入字符串的長度通過內建的len函數取得。
記住,break語句也可以在for循環中使用。
continue語句 continue語句被用來告訴Python跳過當前循環塊中的剩余語句,然后 繼續 進行下一輪循環。
練習:使用continue語句 while True:
s = raw_input('Enter something : ')
if s == 'quit':
break
if len(s) < 3:
continue
print 'Input is of sufficient length'
# Do other kinds of processing here...
練習輸出: Enter something : a
Enter something : 12
Enter something : abc
Input is of sufficient length
Enter something : quit
練習程序是如何工作的? 在這個程序中,我們從用戶處取得輸入,但是我們僅僅當它們有至少3個字符長的時候才處理它們。所以,我們使用內建的len函數來取得長度。如果長度小于3,我們將使用continue語句忽略塊中的剩余的語句。否則,這個循環中的剩余語句將被執行,我們可以在這里做我們希望的任何處理。
注意,continue語句對于for循環也有效。
以下題目可在OJ上完成:
第1題:圖形輸出 2480 題目描述
用基本輸出語句打印以下圖形:
#
##
###
####
#####
######
輸入
本題目沒有輸入數據
輸出
輸出圖形由6行組成,第1行有1個#號,第i行有連續的i個#號:
#
##
###
####
#####
######
示例輸入
示例輸出
#
##
###
####
#####
######
-------------------------
第2題:交換兩個整數的值 題目描述
交換兩個變量的值,由終端輸入兩個整數給變量x、y,然后交換x和y的值后,輸出x和y。
輸入
從鍵盤輸入兩個整數變量x和y;
輸出
在交換x、y的值后將x和y輸出!
示例輸入
4 6
示例輸出
6 4
-----------------------------------------
第3題:Python 循環打印圖形(循環結構) 題目描述
通過使用雙重for循環語句,打印下列圖形:
提交
輸入
輸出
示例輸入
示例輸出
*
***
*****
*******
*****
***
*
-----------------------------------------
第4題:IBM Minus One 題目描述
You may have heard of the book '2001 - A Space Odyssey' by Arthur C. Clarke, or the film of the same name by Stanley Kubrick. In it a spaceship is sent from Earth to Saturn. The crew is put into stasis for the long flight, only two men are awake, and the ship is controlled by the intelligent computer HAL. But during the flight HAL is acting more and more strangely, and even starts to kill the crew on board. We don't tell you how the story ends, in case you want to read the book for yourself :-)
After the movie was released and became very popular, there was some discussion as to what the name 'HAL' actually meant. Some thought that it might be an abbreviation for 'Heuristic ALgorithm'. But the most popular explanation is the following: if you replace every letter in the word HAL by its successor in the alphabet, you get ... IBM.
Perhaps there are even more acronyms related in this strange way! You are to write a program that may help to find this out.
輸入
The input starts with the integer n on a line by itself - this is the number of strings to follow. The following n lines each contain one string of at most 50 upper-case letters.
輸出
For each string in the input, first output the number of the string, as shown in the sample output. The print the string start is derived from the input string by replacing every time by the following letter in the alphabet, and replacing 'Z' by 'A'.
Print a blank line after each test case.
示例輸入
2
HAL
SWERC
示例輸出
String #1
IBM
String #2
TXFSD
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的python如何输入一个数停止输出可循环部分_Python 第04周:控制与循环的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Nikon Rumors称未收到Z 6Ⅲ
- 下一篇: 博主:某厂商折叠机高低配版将用不同处理器