python基礎 流程控制案例

2022-05-29 03:48:14 字數 2492 閱讀 9115

簡單理解編譯型語言類似谷歌翻譯,整篇讀入整篇翻譯,代表語言有c語言,解釋型語言類似同   聲傳譯,讀入一行翻譯一行,代表語言有python。

互動式、**寫入檔案

單行注釋用單引號,多行注釋用三引號

有true和false

1.變數名首個不能為數字

2.變數名收個不能為字元

3.先定義後引用

每個變數值對應乙個id,用id查變數名在記憶體的位址

#

(1)實現使用者輸入使用者名稱和密碼,當使用者名為seven且密碼為123時,顯示登入成功,否則登入失敗!

name = '

senven

'pwd = '

123'

while

true:

name = ipunt('

請輸入使用者名稱!')

pwd = ipunt('

請輸入密碼!')

if name == '

senven

'and pwd == '

123'

:

print('

登入成功!')

else

:

print('

登入失敗!')

#(2)實現使用者輸入使用者名稱和密碼為seven且密碼為123時,顯示登入成功,否則登入失敗,失敗時允許登入三次

count =0

while count <= 3:

name = input('

請輸入使用者名稱!')

pwd = ipunt('

請輸入密碼!')

if name == '

seven

'and pwd == '

123'

:

print('

登入成功!')

else

:

print('

登入失敗!')

count += 1

#(3)實現使用者輸入使用者和密碼,當使用者名為seven或alex且密碼為123時,顯示登入成功,否則登入失敗,失敗時允許重負輸入三次。

count =0

while count <= 3:

name = input('

請輸入使用者名稱!')

pwd = input('

請輸入密碼')

if name == '

seven'or

'alex

'and pwd == '

123'

:

print('

登入成功!')

else

:

print('

登入失敗!')

count += 1

#

a 使用while迴圈實現輸出2-3+4-5+6....+100的和

e = 2s =0

while e <= 100:

if e % 2 ==0:

s +=e

else

: s -=e

e += 1

print

(s)#

b 使用whine迴圈實現輸出1,2,3,4,5,6,7,8,9,11,12,

count = 1

while count < 13:

if count == 10:

count += 1coutinue

print

(count)

count += 1

#c 使用while迴圈實現輸出1-100以內所有的奇數。

count = 1

while count < 101:

if count % 2 ==0:

count += 1

continue

print

(count)

count += 1

#d 使用while迴圈實現輸出1-100內的所有偶數

count = 1

while count < 101:

if count % 2 !=0:

count += 1

continue

print

(count)

count += 1

'''編寫登入介面

基礎要求;

讓使用者輸入使用者名稱密碼

認證成功後顯示歡迎資訊

輸入三次後退出程式

'''count = 1

while count <= 3:

count += 1name = ipunt('

請輸入使用者名稱')

pwd = ipunt('

請輸入密碼')

if name=='

tom'

and pwd=='

123'

:

print('

歡迎進入')

break

Python 流程控制語句案例

雞兔同籠問題 是我國古算書 孫子算經 中著名的數學問題,其內容是 今有雉 雞 兔同籠,上有三十五頭,下有九十四足,問雉兔各幾何 具體實現如例所示。for chicken in range 0 36 if2 chicken 35 chicken 4 94 print 小雞 chicken,小兔 35 ...

python基礎(流程控制)

命名規則 變數名 包名 python推薦 last name 小駝峰 lastname if語句 if 條件 條件成立,做的事情 else 條件不成立,做的事情 elif語句 if 條件 and 條件 成立,則。elif 條件 成立,則。else 以上都不成立,則。且不要空格和tab共用!邏輯判斷 ...

Python基礎 流程控制

1 數字加,2 字串拼接 1.數字相乘 2 字串和整數相乘表示重複字串 取餘 取整 取冪 a b 相當於 a a b a b 相當於 a a b 變數 資料比較位址是否相等 isisnot 簡單資料型別 如果有重複資料 不再開闢新空間,使用原空間位址,從而節約記憶體空間 複雜資料型別 無論資料是否重...