程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

python考試

編輯:Python

6、輸出100以內的質數

for i in range(2, 101):
for j in range(2, i//2+1):
if i % j == 0:
break
else:
print(i)

7、定義函數,判斷用戶名是否合法(至少包含一個大寫字母,一個數
字)

import string
numbers = string.digits
upper = string.ascii_uppercase
name = "csdvvscADA2521s"
def check(name):
num = 0
upp = 0
for i in name:
if i in numbers:
num = 1
if i in upper:
upp = 1
if num == upp == 1:
return True
else:
return False
if check(name):
print("合法")
else:
print("不合法")

8、使用遞歸,輸出指定目錄中所有的jpg文件

import os
dirname = input("請輸入路徑:")
def selectjpg(dirpatth):
items = os.listdir(dirpatth)
for item in items:
item=os.path.join(dirpatth, item)
if os.path.isdir(item):
selectjpg(item)
elif os.path.isfile(item):
if item.endswith(".jpg"):
print(os.path.basename(item))
selectjpg(dirname)

9、異常處理:處理"123"+4的異常

a = "123"
b = 4
try:
c = a + b
except TypeError:
print("類型錯誤")

10、圖書管理系統,要保存的圖書信息包
括:圖書編號、書名、價格、庫存量。鍵盤輸入3本書的信息,分別以三種方式存儲並完成輸出。如果書名超過4個字,只顯示4個字,其余
加‘.…’
方式一:使用元組存儲一本書的信息,元組列表存儲所有圖書
方式二:使用字典存儲所有圖書信息,鍵為圖書編號,值為“書名_價格庫存量”
方式三:定義圖書類(使用property封裝屬性),使用對象列表存儲所有圖書

class book:
def __init__(self, id, name, price, number):
self.__id = id
self.__name = name
self.__price = price
self.__number = number
@property
def id(self):
return self.__id
@id.setter
def id(self, id1):
self.__id = id1
@property
def name(self):
return self.__name
@name.setter
def name(self, name1):
self.__name = name1
@property
def price(self):
return self.__price
@price.setter
def price(self, price1):
self.__price = price1
@property
def number(self):
return self.__number
@number.setter
def price(self, number1):
self.__number = number1
tu = tuple()
dic = {
}
ls = []
for i in range(3):
id = input("請輸入圖書編號:")
name = input("請輸入書名:")
price = int(input("請輸入價格:"))
number = int(input("請輸入庫存:"))
if i == 0:
tu = (id, name, price, number)
elif i == 1:
dic[id] = [name, price, number]
elif i == 2:
ls.append(book(id, name, price, number))
def sl(bookname):
if len(bookname) > 4:
name = bookname[0:4] + "***"
print('名字', name)
else:
print('名字', bookname)
print('圖書編號', tu[0])
sl(tu[1])
print('價格', tu[2])
print('庫存', tu[3])
for id, value in dic.items():
print('圖書編號', id)
sl(value[0])
print('價格', value[1])
print('庫存', value[2])
for i in ls:
print('圖書編號', i.id)
sl(i.name)
print('價格', i.price)
print('庫存', i.number)

11、使用多態模擬:寵物醫生:給寵物看病寵物:狗狗生病了,就打針吃藥;小鳥生病了,就包扎療養


class Animal_doctor:
def ill(self):
print("給寵物看病")
def treat(self,anmial):
anmial.ill()
class Dog(Animal_doctor):
def ill(self):
print("狗生病,打針吃藥")
class Bird(Animal_doctor):
def ill(self):
print("小鳥生病,包扎療養")
doctor = Animal_doctor()
dog = Dog()
bird = Bird()
doctor.treat(dog)
doctor.treat(bird)

12、文件讀寫:
1)隨機產生10個4位數字組成的驗證碼,存儲在code.txt文件中(一個驗證碼一行)
2)從code.txt中讀出所有內容輸出

import string
import random
a = string.digits + string.ascii_letters
def yanz():
code = ""
for i in range(4):
code += random.choice(a)
return code
with open('code.txt','w') as f:
for i in range(10):
f.write(yanz()+'\r')
with open('code.txt','r') as f:
print(f.read())

13、使用爬蟲下載圖片

import requests
url = "https://pic.netbian.com/uploads/allimg/211109/221532-163646733295cd.jpg"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0"}
respones = requests.get(url,headers = headers)
print("正在下載")
with open("E://"+'picture1.jpg','wb') as f:
f.write(respones.content)
print("下載完成")

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved