Python——簡單的A+B#雖然我覺得並不簡單

題目來自NEUQ OJ(2.7,因爲OJ上沒法交其他版本(╯‵□′)╯︵┻━┻)

1002: A+B(基本輸入輸出2)

描述

題目描述:

輸入兩個數A,B,輸出A+B的值。

輸入:

第一行是數據的組數N,從第二行開始是N組由兩個整數(a和b)構成的數據,a和b之間用空格隔開,每組輸入單獨佔一行

輸出:

每組的兩個整數(a和b)求和並輸出,每組的求和結果獨佔一行

樣例輸入

2
1 2
10 20

樣例輸出

3
30

n=input()
count=0
while(count<n):
    try:
       print sum(map(int,raw_input().split())) 
    except:
        break

以上,代碼

下面針對幾個我開始不太理解,後來百度加問學長得到的幾點信息:

       print sum(map(int,raw_input().split())) 


raw_input()輸入整行數據

raw_input().splint()將輸入的整行數據從空格處切開

map(int,raw_input().splint())將輸入的字符轉化爲數字

sum(map(int,raw_input().splint()))最後求和

對於map()它的原型是:map(function,sequence),就是對序列sequence中每個元素都執行函數function操作。
比如之前的a,b,c = map(int,raw_input().split()),意思就是說把輸入的a,b,c轉化爲整數。

②try   &   except

有時候我們寫程序的時候,會出現一些錯誤或異常,導致程序終止。例如,做除法時,除數爲0,會引起一個ZeroDivisionError

例子:

a=10
b=0
c=a/b
print "done"



運行結果:

Traceback (most recent call last):
File "C:/Users/lirong/PycharmProjects/untitled/openfile.py", line 3, in <module>
c=a/b
ZeroDivisionError: integer division or modulo by zero

我們發現程序因爲ZeroDivisionError而中斷了,語句print "done" 沒有運行。爲了處理異常,我們使用try...except,更改代碼:

a=10
b=0
try:
    c=a/b
    print c
except ZeroDivisionError,e:
    print e.message
print "done"


運行結果:

integer division or modulo by zero
done

這樣程序就不會因爲異常而中斷,從而print "done"語句正常執行。

我們把可能發生錯誤的語句放在try模塊裏,用except來處理異常。except可以處理一個專門的異常,也可以處理一組圓括號中的異常,如果except後沒有指定異常,則默認處理所有的異常。每一個try,都必須至少有一個except

處理一組異常可以這樣寫(其中e代表異常的實例):

try:
    pass
except (IOError ,ZeroDivisionError),e:
    print e


try ....except...else 語句,當沒有異常發生時,else中的語句將會被執行。

例子:

a=10
b=0
try:
    c = b/ a
    print c
except (IOError ,ZeroDivisionError),x:
    print x
else:
    print "no error"
print "done"


運行結果:

0
no error
done

----------------------------------------------------------------------以上,來自HZ學長以及博客:https://www.cnblogs.com/Lival/p/6203111.html

最後,按照慣例,再介紹一個單詞 逃)

split 1
 /splɪt,splɪt/ v. S2 W3
 [splɪt] 
pp: split   pt: split   pres part: splitting 

MEANINGS 義項

  • 1.

    DISAGREE 不同意

    [I,T] if a group of people splits, or if it is split, people in the group disagree strongly with each other and the group sometimes divides into separate smaller groups

    〔組織內部〕產生分歧;分裂

    THESAURUS 

    BREAK SEPARATE TEAR

    It was feared that the issue would split the church. 人們擔心這一問題會導致教會分裂。

    be split on/over sth

    The party is split over the issue of immigration. 該黨在移民問題上意見有分歧。

    The government appears deeply split on this issue. 政府內部在這個問題上似乎有嚴重的分歧。

    [+ from]

    The Pan-Africanist Congress split from the ANC in 1959. 泛非主義者大會在 1959 年脫離了非洲人國民大會。

    split sth in two/down the middle

    The war has split the nation in two. 戰爭使這個國家一分爲二。

  • 2.

    SEPARATE INTO PARTS 分割成幾部分

    [I,T] to divide or separate something into different parts or groups, or to be divided into different parts or groups

    分割,把…分開〔成幾部分〕;分成〔不同部分〕

    [+ into]

    Can you split into groups of three now? 現在你們分成三人一組好嗎?

    split sth into sth

    The book is split into six sections. 本書共分六個部分。

  • 3.

    BREAK OR TEAR 裂開或撕開

    [I,T] if something splits, or if you split it, it tears or breaks along a straight line

    (使)裂開,(使)撕開,(使)斷裂

     see picture at  break 1

    The branch split under their weight. 樹枝承受不住他們的重量斷裂了。

    One of the boxes had split open . 其中一個盒子裂開了。

    Split the pineapple down the middle . 把菠蘿從中間切開。

    split (sth) in two/half

    The board had split in two. 木板裂成了兩塊。

  • 4.

    SHARE 分攤

    [T] to divide something into separate parts and share it between two or more people

    均分,分配

    We agreed to split the cost . 我們同意分攤這筆費用。

    split sth between sb/sth

    Profits will be split between three major charities. 盈利將在三大慈善機構中間分配。

    split sth with sb

    He agreed to sell the car and split the proceeds with his brother. 他同意把車賣掉,所得的錢和弟弟分。

    split sth three/four etc ways (=share something between three, four etc people or groups)將某物分成三份/四份等

    The money will have to be split three ways. 這筆錢必須分成三份。

  • 5.

    INJURE 損傷

    [T] to make someone’s head or lip have a cut in it, as a result of a fall or hit

    碰破〔頭或嘴脣〕,把…碰得裂開

    She fell against a table and split her lip . 她摔倒時撞到桌子,磕破了嘴脣。

    The force of the blow nearly split his head open . 這一擊打得他腦袋都快裂開了。

  • 6.

    END RELATIONSHIP 結束關係

    [I] informal if people split, they end a marriage or relationship with each other

    分手;離婚;決裂

    The band split two years ago. 這支樂隊兩年前解散了。

    [+ with/from]

    He split from his wife last year. 去年他和妻子離了婚。

  • 7.

    LEAVE 離開

    [I] old-fashioned,informal to leave a place quickly

    迅速離開

    Come on – let’s split. 快,我們快走。

  • 8.

    split hairs

    to argue that there is a difference between two things, when the difference is really too small to be important

    糾纏於細枝末節,在瑣細的問題上爭辯

    This is just splitting hairs. 這就太斤斤計較了。

  • 9.

    split the difference

    to agree on an amount that is exactly between two amounts that have been mentioned

    〔在數額上〕互相讓步,折中,妥協

    OK, let’s split the difference, and I’ll give you £20. 行,我們各讓一步,我給你 20 英鎊。

  • 10.

    split your sides

    informal to laugh a great deal

    捧腹大笑

    收起

PHRVB 短語動詞

  • split offphr v

    1.

    if one part of something splits off from the rest, it becomes completely separate from it

    脫開,分離

    • A huge lump of rock had split off from the cliff face.一大塊岩石從崖面上脫落下來。

    2.

    if a small group of people split off from a larger group, they become separate from it

    〔從羣體中〕分裂出去,脫離

    • The group split away from the Green Party and formed the Environmental Alliance.這批人脫離綠黨,組成了環境聯盟。

    3.

    split sth←→off to separate one part of something and make it completely separate from the rest

    使某物脫離出去,使某物分離出去

    • This part of the business has now been split off from the main company.這部分業務現在已經從總公司獨立出去了。

  • split on sbphr vBrE

    1.

    to tell someone in authority about something wrong that someone else has done

    告發

    Don’t you dare split on us! 諒你不敢告發我們!

  • split upphr v

    1.

    if people split up, or if someone splits them up, they end a marriage or relationship with each other

    離婚;分手,斷絕關係;拆散

    Steve’s parents split up when he was four. 史蒂夫四歲時父母離異。

    • I thought she’d split up with her boyfriend.我以爲她已經和男朋友分手了。

    • split sb ←→ up

      Why would she try to split us up?她爲什麼要拆散我們?

    2.

    to divide people into different groups, or to be divided into groups

    把…分成小組;分組

    Please don’t split up when we get to the museum. 到博物館後請大家不要散開。

    • split sth/sb ←→ up

      The teacher split up the class into three groups.老師把全班同學分成三組。

    3.

    split sth←→up to divide something into different parts

    把某物分成若干部分

    • The house has now been split up into individual flats.這棟房子現在被分隔成若干獨立的套間。

split 2
 n. [C ]
 

MEANINGS 義項

  • 1.

    TEAR 裂口

    a tear or crack in something made of cloth, wood etc

    裂口,裂縫

    [+ in]

    a long split in the sleeve of his coat 他外套袖子上一條長長的裂縫

  • 2.

    DISAGREEMENT 意見不一

    a serious disagreement that divides an organization or group of people into smaller groups

    〔團體內部的〕分裂,分歧

    SYN RIFT 

    [+ in/within]

    The argument could lead to a damaging split in the party. 這場爭吵可能會導致黨內出現分裂,對該黨產生不利影響。

    a deep split within the government 政府內部很深的分歧

    [+ between]

    a split between the radicals and the moderates within the group 團體內部激進派和溫和派之間的分歧

    [+ over]

    The union is desperate to avoid a split over this issue. 工會竭力避免在這一問題上發生分歧。

  • 3.

    end of relationship 關係的結束

    informal the end of a marriage or relationship - used especially in newspapers and magazines

    離婚,分手〔尤用於報紙或雜誌〕

    rumours of a marriage split 婚姻破裂的傳言

    [+ with]

    She seems to be getting over her recent split with her fiancé. 她不久前和未婚夫分手,現在似乎正在慢慢走出陰影。

  • 4.

    DIVIDING STH 分某物

    the way in which something, especially money, is shared between several people

    〔尤指錢的〕分配

    In a publishing deal, the average split used to be 50:50 between writer and publisher. 出版協議中,作者和出版商之間一般都是五五分成。

    three-way/four-way etc split (=when something is shared equally between three, four etc people)三人/四人等均分

    a three-way split in the profits 利潤三人均分

  • 5.

    SEPARATION 分開

    informal a clear separation or difference between two things

    分離;分裂

    [+ between]

    the traditional split between the state and church 傳統上的政教分離

  • 6.

    do the splits

    to spread your legs wide apart so that your legs touch the floor along their whole length

    劈叉,劈一 字腿

    收起

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章