Python學習筆記(八)Python練習題一

2022-08-26 04:42:08 字數 2263 閱讀 4060

1.使用while迴圈輸出1.2.3.4.5.6.8.9.10

2.求1-100的所有數之和

3.輸出1-100的內所有奇數

4.輸出1-100內的所有偶數

5.求1-2+3-4+5.。。。99之和

6.使用者登入(3次機會)

解答:1. 

#!

/usr/bin python

#-*-coding:utf8-*-

count = 1

while count <=10:

if count!=7:

print

(count)

count=count+1

"""或者 if count == 7:

pass

else:

print(count)

2.

#!

/usr/bin python

#-*-coding:utf8-*-

count=1n=1

while count<100:

count=count+1n=n+count

print(n)

3.

#!

/usr/bin python

#-*-coding:utf8-*-

count=1n=1

while count<99:

count=count+2

print(n)

"""count=0

while count<100:

count=count+1

if count%2 ==0:

pass

else:

print(count)

4.

#!

/usr/bin python

#-*-coding:utf8-*-

count=2n=2

while count<100:

count=count+2n=n+count

print(n)

"""

count=0

while count<100:

count=count+1

if count%2 ==0:

print(count)

else:

pass

5.

#!

/usr/bin python

#-*-coding:utf8-*-

count=1n=1

while count<99:

count=count+1

if count%2 ==0:

n=n-count

else

: n=n+count

print(n)

#!

/usr/bin python

#-*-coding:utf8-*-

count=3

while count!=0:

count=count-1inp1=input("

請輸入使用者:")

inp2=input("

請輸入密碼:")

if inp1=="

zengtao":

if inp2=="zt"

:

print("

正確,歡迎

",inp1)

count=0

else

:

print("

密碼錯誤,請重試")

print("

您還有",count,"

次機會"

)

else

:

print("

使用者或密碼錯誤,請重試")

print("

您還有",count,"

次機會")

"""count = 0

while count < 3:

user = input('>>>')

pwd = input('>>>')

if user == 'alex' and pwd == '123':

print('歡迎登陸')

print('..........')

break

else:

print('使用者名稱或者密碼錯誤')

count = count + 1

補充:字元型數字轉換為數字

inp=int()

Python學習筆記(八)

二 字串 1 概述 pickle模組為python物件結構的序列化和反序列化實現了乙個基本但強大的演算法。pickling 是將python物件層次結構轉換為位元組流的過程,unpickling 是相反的操作,即位元組流轉換回物件層次結構。2 常用方法 1 序列化為字串 pickle.dumps o...

Python學習筆記(八)

對應第十章 如果檔案內容不是逐行輸出則會輸出列表形式。try with open d pycharmprograms pi.txt as f contents f.readlines except filenotfounderror print filenotfounderror else prin...

Python學習筆記(八)異常

8異常 8.1什麼是異常 python用異常物件來表示異常情況。每乙個異常都是一些類的例項,這些例項可以被印發,並且可以用很多種方法進行捕捉並且對其進行處理,而不是讓整個程式失敗。8.2按自己的方式出錯 8.2.1raise語句 為了引發異常,可以使用乙個類 可以是exception的子類 或者例項...