1. 程式人生 > >3.運算子與表示式,控制流

3.運算子與表示式,控制流

時間:2018年12月8日14:56:25

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

表示式包括運算子與運算元

1、運算子

2、求值順序

控制流(3種)

1、if語句

number = 23
guess = int(input('enter an number:'))
if guess == number:  #注意別忘記冒號
    print('yes!')
    print('(but you do not win any prizes!)')
elif guess < number:
    print('no,it is a little higher than that')
else:
    print('no, it is a little lower than that')
print('done')  #這一句在if結束後執行

  2.while語句

number = 23
running = True

while running:
    guess = int(input('enter an number:'))
    if guess == number:  #注意別忘記冒號
        print('yes!')
        running = False
    elif guess < number:
        print('no,it is a little higher than that')
    else:
        print('no, it is a little lower than that')
else:
    print('while is over.')
print('done')  #這一句在if結束後執行

3、for迴圈

for i in range(1,5):
    print(i)
else:
    print('the for loop is over')

4、break語句

        用於中斷迴圈語句 的執行,如果中斷了for或while迴圈,相應迴圈中的else塊都將不會被執行。

while True:
    s = input('Enter something:')
    if s == 'quit':
        break#跳出while迴圈
    print('length of the string is',len(s))
print('done')

5、continue語句

       跳過當前迴圈塊中的剩餘語句,並繼續該迴圈的下一次迭代。

while True:
    s = input('enter somethong:')
    if s == 'quit':
        break#退出while迴圈
    if len(s)<3:
        print('too small')
        continue#繼續下一次while
    print('input is of sufficient length')
print('done')

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

結束:2018年12月8日17:06:40

進度:59/153