5道经典的Python笔试题【杭州多测师】【杭州多测师_王sir】

import requests

class Save:

    def api(self):
        url = 'https://www.baidu.com/'
        headers = {"Accept": "application/json, text/javascript, */*; q=0.01"}
        response = requests.post(url,headers=headers)
        return (response.text)

    def save(self,path,filename):
        with open(path,mode='w',encoding='utf-8') as f:
            f.write(filename)
            f.close()

m = Save()
m.save('./logs/baidu.txt',m.api())

d  =  { 'name' :小明 'age' :12print  d.has_key( 'name' )
#结果返回True

 

def add(x,y):

    '''相加'''

    return x + y
print(add(1,2))

 

def printstar(n ):
    for  i  in range(1,n+1,2):
        print ((' ' +'*'*i).center(10))
        if  i == n:
            for x in range(1,i+1,2):
                print ((' '+'*'*(n-x-1)).center(10))
printstar(5)

 

import json
str1 = '{"result":[{"vendor_id":"726","vendor_name":"Scusa"},' \
       '{"vendor_id":"519","vendor_name":"Emilie French Restaurant and Bar"}]}'
dict1 = eval(str1)  #转换为字典

def func(value):
    for k,v in value.items():
        if isinstance(v,dict):
            func(v)
        else:
            if isinstance(v,list):
                for j in v:
                    for m,n in j.items():
                        print(m,n)

func(dict1)

 

posted @ 2021-11-15 15:20  多测师_树哥  阅读(100)  评论(0编辑  收藏  举报