日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python如何输入一个数停止输出可循环部分_Python 第04周:控制与循环

發(fā)布時間:2023/12/19 python 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python如何输入一个数停止输出可循环部分_Python 第04周:控制与循环 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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語句允許你重復執(zhí)行一塊語句。while語句是所謂 循環(huán) 語句的一個例子。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

練習程序是如何工作的?

在這個程序中,我們?nèi)匀皇褂昧瞬聰?shù)游戲作為例子,但是這個例子的優(yōu)勢在于用戶可以不斷的猜數(shù),直到他猜對為止——這樣就不需要像前面那個例子那樣為每次猜測重復執(zhí)行一遍程序。這個例子恰當?shù)卣f明了while語句的使用。 我們把raw_input和if語句移到了while循環(huán)內(nèi),并且在while循環(huán)開始前把running變量設置為True。首先,我們檢驗變量running是否為True,然后執(zhí)行后面的 while-塊 。在執(zhí)行了這塊程序之后,再次檢驗條件,在這個例子中,條件是running變量。如果它是真的,我們再次執(zhí)行while-塊,否則,我們繼續(xù)執(zhí)行可選的else-塊,并接著執(zhí)行下一個語句。

當while循環(huán)條件變?yōu)镕alse的時候,else塊才被執(zhí)行——這甚至也可能是在條件第一次被檢驗的時候。如果while循環(huán)有一個else從句,它將始終被執(zhí)行,除非你的while循環(huán)將永遠循環(huán)下去不會結束!

True和False被稱為布爾類型。你可以分別把它們等效地理解為值1和0。在檢驗重要條件的時候,布爾類型十分重要,它們并不是真實的值1。

else塊事實上是多余的,因為你可以把其中的語句放在同一塊(與while相同)中,跟在while語句之后,這樣可以取得相同的效果。

for循環(huán) for..in是另外一個循環(huán)語句,它在一序列的對象上 遞歸 即逐一使用隊列中的每個項目。我們會在后面的章節(jié)中更加詳細地學習序列。

練習: 使用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

練習程序是如何工作的? 在這個程序中,我們打印了一個 序列 的數(shù)。我們使用內(nèi)建的range函數(shù)生成這個數(shù)的序列。

我們所做的只是提供兩個數(shù),range返回一個序列的數(shù)。這個序列從第一個數(shù)開始到第二個數(shù)為止。例如,range(1,5)給出序列[1, 2, 3, 4]。默認地,range的步長為1。如果我們?yōu)閞ange提供第三個數(shù),那么它將成為步長。例如,range(1,5,2)給出[1,3]。記住,range 向上 延伸到第二個數(shù),即它不包含第二個數(shù)。

for循環(huán)在這個范圍內(nèi)遞歸——for i in range(1,5)等價于for i in [1, 2, 3, 4],這就如同把序列中的每個數(shù)(或?qū)ο?賦值給i,一次一個,然后以每個i的值執(zhí)行這個程序塊。在這個例子中,我們只是打印i的值。

記住,else部分是可選的。如果包含else,它總是在for循環(huán)結束后執(zhí)行一次,除非遇到break語句。

記住,for..in循環(huán)對于任何序列都適用。這里我們使用的是一個由內(nèi)建range函數(shù)生成的數(shù)的列表,但是廣義說來我們可以使用任何種類的由任何對象組成的序列!我們會在后面的章節(jié)中詳細探索這個觀點。

break語句 break語句是用來 終止 循環(huán)語句的,即哪怕循環(huán)條件沒有稱為False或序列還沒有被完全遞歸,也停止執(zhí)行循環(huán)語句。

一個重要的注釋是,如果你從for或while循環(huán)中 終止 ,任何對應的循環(huán)else塊將不執(zhí)行。

練習: 使用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'。通過 終止 循環(huán)到達程序結尾來停止程序。

輸入字符串的長度通過內(nèi)建的len函數(shù)取得。

記住,break語句也可以在for循環(huán)中使用。

continue語句 continue語句被用來告訴Python跳過當前循環(huán)塊中的剩余語句,然后 繼續(xù) 進行下一輪循環(huán)。

練習:使用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個字符長的時候才處理它們。所以,我們使用內(nèi)建的len函數(shù)來取得長度。如果長度小于3,我們將使用continue語句忽略塊中的剩余的語句。否則,這個循環(huán)中的剩余語句將被執(zhí)行,我們可以在這里做我們希望的任何處理。

注意,continue語句對于for循環(huán)也有效。

以下題目可在OJ上完成:

第1題:圖形輸出 2480 題目描述

用基本輸出語句打印以下圖形:

#

##

###

####

#####

######

輸入

本題目沒有輸入數(shù)據(jù)

輸出

輸出圖形由6行組成,第1行有1個#號,第i行有連續(xù)的i個#號:

#

##

###

####

#####

######

示例輸入

示例輸出

#

##

###

####

#####

######

-------------------------

第2題:交換兩個整數(shù)的值 題目描述

交換兩個變量的值,由終端輸入兩個整數(shù)給變量x、y,然后交換x和y的值后,輸出x和y。

輸入

從鍵盤輸入兩個整數(shù)變量x和y;

輸出

在交換x、y的值后將x和y輸出!

示例輸入

4 6

示例輸出

6 4

-----------------------------------------

第3題:Python 循環(huán)打印圖形(循環(huán)結構) 題目描述

通過使用雙重for循環(huán)語句,打印下列圖形:

提交

輸入

輸出

示例輸入

示例輸出

*

***

*****

*******

*****

***

*

-----------------------------------------

第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

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的python如何输入一个数停止输出可循环部分_Python 第04周:控制与循环的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 五个女闺蜜把我玩到尿失禁 | 在线精品自拍 | 男人的天堂a在线 | 91精品日韩 | 亚洲不卡中文字幕 | 成人91| 亚洲精品v天堂中文字幕 | 中国少妇高潮 | 国产色a| 久草五月天| 国产一级片免费在线观看 | 亚洲自拍小视频 | 亚洲丁香花色 | 日本中文一区 | 午夜一区二区三区在线观看 | 欧美专区在线 | 白丝一区 | 四虎国产视频 | 精品国产乱码一区二区三区99 | xxx黄色片 | 日在线视频 | 国产成人亚洲欧洲在线 | 三级网站免费看 | 在线麻豆视频 | 长腿校花无力呻吟娇喘的视频 | 天天干天天插 | 国产偷国产偷av亚洲清高 | 天堂在线亚洲 | 久久久久久国产精品三区 | 成人综合婷婷国产精品久久 | 色视频网站在线观看 | 青青草超碰在线 | 香蕉网站在线观看 | 欧美乱大交xxxxx潮喷 | 五号特工组之偷天换月 | 中文字幕精品在线视频 | 日本视频久久 | √天堂中文官网8在线 | 免费在线观看污网站 | 人妻一区二区三区视频 | 国产精品久久久久久久久久久久久久 | 影音先锋成人资源网 | 久久久久九九九九 | 国产在线你懂得 | 国产在线导航 | 91精品国产一区二区在线观看 | 五月天丁香激情 | 国产欧美日韩一区二区三区 | 亚洲欧洲综合网 | 国产jjizz一区二区三区视频 | 一久久久 | av少妇| 午夜不卡影院 | 欧美久久综合 | 在线91av | 国产精品va在线观看无码 | 九九九九精品九九九九 | wwwwxxxx欧美 | 幸福,触手可及 | 强videoshd酒醉 | 精品国产aⅴ一区二区三区东京热 | 国产三级在线观看完整版 | 国产91亚洲 | 亚洲图片88 | 日韩三级免费观看 | aaa黄色 | 天堂bt在线 | 亚洲在线a | 日韩精品免费一区二区 | 天堂一区二区三区 | 日韩免费网 | 一区二区三区国产 | 影音先锋天堂网 | 国产做爰免费视频观看 | 欧美日韩在线视频一区二区 | 国产精品视频你懂的 | 成人黄色短片 | 五月天天| 蜜桃免费在线视频 | 欧美日韩国产图片 | 日本视频网址 | 国产真实乱偷精品视频 | 免费日韩av | 国产中文字幕视频 | 国产精品人人妻人人爽人人牛 | 在线亚洲一区二区 | 蜜色av | 丰满少妇被猛烈进入一区二区 | 日韩在线你懂的 | 久久逼逼 | 韩国av不卡| 日本一区二区在线免费 | 制服丝袜天堂 | 亚洲一线av | 久久成人国产精品 | 91区国产 | 国产精彩视频一区 | 天天干在线播放 | 亚洲精品社区 |