Python——簡單A+B

題目來自NEUQ OJ(2.7)

1003: A+B(基本輸入輸出3)

描述

題目描述:

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

輸入:

多組數據:每組由兩個整數(a和b)構成,a和b之間用空格隔開,每組輸入單獨佔一行。

當輸入爲 0 0 時,輸入結束。0 0這組數據不處理。

輸出:

對於每一組測試用例,輸出齊對應的和,每組數據一行。

樣例輸入

1 2
3 4
10 20
0 0

樣例輸出

3
7
30

代碼如下:

while(1):
    try:
        a,b=map(int,raw_input().split())
        if a!=0 or b!=0:
            sum=a+b
            print sum 
        elif a==0 and b==0:
            break;
    except:
        break

前幾天聽一個大佬教育我,學一個語言可以先不要系統的學,先學習框架,然後在做題的時候找到自己需要什麼,然後再通過百度啊問大佬啊,這樣的方法,效果會更好。

然後,我就聽取了大佬的意見,果然是受益匪淺。

閒言少敘,就來解析一下這段代碼需要的知識吧!

while循環:

(資料來源於http://www.runoob.com/python/python-while-loop.html)

while 判斷條件:
    執行語句……


實例
#!/usr/bin/python
 
count = 0
while (count < 9):
   print 'The count is:', count
   count = count + 1
 
print "Good bye!"


輸出:

The count is: 0

The count is: 1

The count is: 2

The count is: 3

The count is: 4

The count is: 5

The count is: 6

The count is: 7

The count is: 8

Good bye!



while 語句時還有另外兩個重要的命令 continue,break 來跳過循環,continue 用於跳過該次循環,break 則是用於退出循環,此外"判斷條件"還可以是個常值,表示循環必定成立,具體用法如下:
# continue 和 break 用法
 
i = 1
while i < 10:   
    i += 1
    if i%2 > 0:     # 非雙數時跳過輸出
        continue
    print i         # 輸出雙數2、4、6、8、10
 
i = 1
while 1:            # 循環條件爲1必定成立
    print i         # 輸出1~10
    i += 1
    if i > 10:     # 當i大於10時跳出循環
        break



如果條件判斷語句永遠爲 true,循環將會無限的執行下去,如下實例:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
var = 1
while var == 1 :  # 該條件永遠爲true,循環將無限執行下去
   num = raw_input("Enter a number  :")
   print "You entered: ", num
 
print "Good bye!"


以上實例輸出結果:

Enter a number  :20
You entered:  20
Enter a number  :29
You entered:  29
Enter a number  :3
You entered:  3
Enter a number between :Traceback (most recent call last):
  File "test.py", line 5, in <module>
    num = raw_input("Enter a number :")
KeyboardInterrupt

注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。

‘’‘

這裏補充一句,我延續了c++裏的寫法,即while(1)

’‘’

在 python 中,while … else 在循環條件爲 false 時執行 else 語句塊:

#!/usr/bin/python
 
count = 0
while count < 5:
   print count, " is  less than 5"
   count = count + 1
else:
   print count, " is not less than 5"


以上實例輸出結果爲:

0 is less than 5
1 is less than 5
2 is less than 5
3 is less than 5
4 is less than 5
5 is not less than 5



類似 if 語句的語法,如果你的 while 循環體中只有一條語句,你可以將該語句與while寫在同一行中, 如下所示:

#!/usr/bin/python
 
flag = 1
 
while (flag): print 'Given flag is really true!'
 
print "Good bye!"


注意:以上的無限循環你可以使用 CTRL+C 來中斷循環。

if  &&  else

以下內容來自http://www.runoob.com/python/python-if-statement.html

Python程序語言指定任何非0和非空(null)值爲true,0 或者 null爲false。

Python 編程中 if 語句用於控制程序的執行,基本形式爲:

if 判斷條件:
    執行語句……
else:
    執行語句……

其中"判斷條件"成立時(非零),則執行後面的語句,而執行內容可以多行,以縮進來區分表示同一範圍。

else 爲可選語句,當需要在條件不成立時執行內容則可以執行相關語句,具體例子如下:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 例1:if 基本用法
 
flag = False
name = 'luren'
if name == 'python':         # 判斷變量否爲'python'
    flag = True          # 條件成立時設置標誌爲真
    print 'welcome boss'    # 並輸出歡迎信息
else:
    print name              # 條件不成立時輸出變量名稱

輸出結果爲:

luren            # 輸出結果

if 語句的判斷條件可以用>(大於)、<(小於)、==(等於)、>=(大於等於)、<=(小於等於)來表示其關係。

當判斷條件爲多個值時,可以使用以下形式:

if 判斷條件1:
    執行語句1……
elif 判斷條件2:
    執行語句2……
elif 判斷條件3:
    執行語句3……
else:
    執行語句4……
實例如下:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 例2:elif用法
 
num = 5     
if num == 3:            # 判斷num的值
    print 'boss'        
elif num == 2:
    print 'user'
elif num == 1:
    print 'worker'
elif num < 0:           # 值小於零時輸出
    print 'error'
else:
    print 'roadman'     # 條件均不成立時輸出

輸出結果爲:

roadman        # 輸出結果

由於 python 並不支持 switch 語句,所以多個條件判斷,只能用 elif 來實現,如果判斷需要多個條件需同時判斷時,可以使用 or (或),表示兩個條件有一個成立時判斷條件成功;使用 and (與)時,表示只有兩個條件同時成立的情況下,判斷條件才成功。

#!/usr/bin/python
# -*- coding: UTF-8 -*-
 
# 例3:if語句多個條件
 
num = 9
if num >= 0 and num <= 10:    # 判斷值是否在0~10之間
    print 'hello'
# 輸出結果: hello
 
num = 10
if num < 0 or num > 10:    # 判斷值是否在小於0或大於10
    print 'hello'
else:
    print 'undefine'
# 輸出結果: undefine
 
num = 8
# 判斷值是否在0~5或者10~15之間
if (num >= 0 and num <= 5) or (num >= 10 and num <= 15):    
    print 'hello'
else:
    print 'undefine'
# 輸出結果: undefine
當if有多個條件時可使用括號來區分判斷的先後順序,括號中的判斷優先執行,此外 and 和 or 的優先級低於>(大於)、<(小於)等判斷符號,即大於和小於在沒有括號的情況下會比與或要優先判斷。

邏輯運算符
以下內容來自:http://www.yiibai.com/python/logical_operators_example.html

Python語言支持以下邏輯運算符。假設變量a的值爲True,變量b的值爲False,那麼 -

運算符 描述 示例
and 如果兩個操作數都爲真,則條件成立。 (a and b)的結果爲False
or 如果兩個操作數中的任何一個非零,則條件成爲真。 (a or b)的結果爲True
not 用於反轉操作數的邏輯狀態。 not(a and b) 的結果爲True


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