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

    劈叉,劈一 字腿

    收起

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