import math

隆昌網站建設公司創新互聯,隆昌網站設計制作,有大型網站制作公司豐富經驗。已為隆昌1000多家提供企業網站建設服務。企業網站搭建\外貿營銷網站建設要多少錢,請找那個售后服務好的隆昌做網站的公司定做!
a=int(input('請輸入a的值:'))
b=int(input('請輸入b的值:'))
c=int(input('請輸入c的值:'))
derta=(b**2-4*a*c)
if a == 0:
print('您輸入的a為0,方程ax^2+bx+c=0不能作為一元二次方程')
elif derta0:
print('方程無實數解')
elif derta==0:
print('方程有解,有一個實數解:')
print(b*(-1)/(a*2))
else:
print('方程有解,有兩個實數解:')
print((-1*b+math.sqrt(derta))/2*a)
print((-1*b-math.sqrt(derta))/2*a)
max_y=max(list(map(lambda x:[x,x*(x-1)], [x for x in range(0,32)])))
print(f"[x,y]={max_y=}")
'''python運行效果
[x,y]=max_y=[31, 930]
'''
找了一圈NBA文字直播網站,發現手機版直播吧有現成的接口,直接返回json格式數據。那就是它了,聽我慢慢道來。
首先在電腦瀏覽器打開手機版直播吧,我用的是chrome瀏覽器,在Network中可以看到,它不停地用GET方式請求,這個地址會返回當前正在進行的所有類型比賽的基本情況,根據其中的type字段過濾掉非NBA比賽就OK了。其中最重要的是ID字段,之后的所有操作都需要用到。返回的數據如下所示:
{
"code": "2760624",
"second": "10",
"list": [
{
"id": "96233",
"sdate": "2017-04-20",
"time": "10:30",
"url": "/zhibo/nba/2017/042096233.htm",
"type": "basketball",
"start": "2017-04-20 10:30",
"home_team": "勇士",
"visit_team": "開拓者",
"home_score": "106",
"visit_score": "81",
"period_cn": "第4節\n01:30",
"from": "dc.live",
"code": "373",
"update": "13:13:37",
"big_score_1": "",
"big_score_2": ""
},
... # 省略了其它比賽的信息
]}12345678910111213141516171819202122232425
獲得所有正在進行的比賽ID后,點擊某一場比賽,進入文字直播頁面。首先請求頁面,其中XXXX是上一步獲取的id,它會返回一個數字,即max_sid。然后判斷該max_sid是否大于上次獲取的該值,如果大于,表示有新的直播文字,否則表示沒有。
如果max_sid大于上一次的值,通過請求(其中XXXX是今天的日期,格式為2017-04-20,YYYY是第一步中獲取的id),返回這場比賽的基本情況,比如比分,是第幾節等,如下所示:
{ "id": "96233", "home_team": "勇士", "visit_team": "開拓者", "home_score": "110", "visit_score": "81", "period_cn": "第4節結束", ...}123456789
最后,就可以獲取直播的文字了。請求(其中XXXX是比賽id,YYYY是max_sid),它會返回最新的直播文字,其中包括一條或多條直播文字,如下所示:
[
{ "live_id": "8769977", "live_text": "@仙女最庫阿-:庫里正負值最高32我去!!!!", "home_score": "110", "visit_score": "81", "pid_text": "比賽結束", ...
}, ... # 可能有多條直播文字]1234567891011
可以看到,該請求返回的信息中沒有比賽剩余時間、主隊和客隊等信息,所以每次獲取直播文字之前,需要多一次請求,獲得比賽的基本信息。
基本流程就是這樣,非常簡單,一共就四個GET請求,返回四串json,用requests庫請求,然后解析搞定。
先定義一個Match類,表示當前正在進行的每一場比賽。
# match.pyclass Match:
def __init__(self, **kwargs):
self.id = kwargs['id']
self.home_team = kwargs['home_team']
self.visit_team = kwargs['visit_team']
self.home_score = kwargs['home_score']
self.visit_score = kwargs['visit_score']
self.period_cn = kwargs['period_cn'].replace('\n', ' ') def __repr__(self):
return '{self.id} {self.home_team} {self.home_score} - {self.visit_score} {self.visit_team} {self.period_cn}'.format(self=self)12345678910111213
再定義一個TextLiving類,表示獲取的每一條文字直播。
# text_living.pyclass TextLiving:
def __init__(self, match_info, **kwargs):
self.home_team = match_info['home_team']
self.visit_team = match_info['visit_team']
self.period_cn = match_info['period_cn']
self.live_text = kwargs['live_text']
self.home_score = kwargs['home_score']
self.visit_score = kwargs['visit_score'] def __repr__(self):
return '{self.home_team} {self.home_score} - {self.visit_score} {self.visit_team} {self.period_cn}\n{self.live_text}\n{sep}'.format(self=self, sep='*'*60)12345678910111213
接著創建zhibo8_api.py模塊,用于獲取相關數據。
# 當前正在進行的比賽Living_Matches_Url = ''# 某一場比賽當前的max_sidMatch_Max_Sid_Url = ''# 某一場比賽最新文字直播Match_Living_Text_Url = ''# 某一場比賽當前的基本情況Match_Info_Url = ''def get_living_matches():
response = requests.get(Living_Matches_Url)
result = json.loads(response.text)
matches = [Match(**match) for match in result['list'] if match['type'] == 'basketball' and match['period_cn'] != '完賽'] return matchesdef get_match_max_sid(match_id):
response = requests.get(Match_Max_Sid_Url % match_id) if response.status_code == requests.codes.ok: return int(response.text)def get_match_living(match_id, max_sid):
# 先獲取比賽的當前情況,再獲取最新文字直播
match_info = get_match_info(match_id)
response = requests.get(Match_Living_Text_Url % (match_id, max_sid))
texts = [] if response.status_code == requests.codes.ok:
result = json.loads(response.text)
texts = [TextLiving(match_info, **living) for living in result] return textsdef get_match_info(match_id):
today = datetime.now().strftime('%Y-%m-%d')
response = requests.get(Match_Info_Url % (today, match_id))
match_info = json.loads(response.text) return match_info123456789101112131415161718192021222324252627282930313233343536373839404142
最后,在main.py模塊中啟動程序,開始直播!
def get_living_matches():
matches = zhibo8_api.get_living_matches() for match in matches:
print(match) return matchesdef get_watch_match(matches):
match_id = input('請輸入比賽ID:') for match in matches: if match.id == match_id: return match else:
print('輸入的ID不正確') return Nonedef main_loop():
matches = get_living_matches() if len(matches) == 0:
print('當前沒有比賽!!!') return
match = get_watch_match(matches) if not match:
print('沒去找到該比賽') return
current_match_max_sid = -1
while True:
match_max_sid = zhibo8_api.get_match_max_sid(match.id) if not match_max_sid:
print('沒有直播數據') return
if current_match_max_sid == match_max_sid: continue
current_match_max_sid = match_max_sid
text_livings = zhibo8_api.get_match_living(match.id, current_match_max_sid) for text in text_livings:
print(text)if __name__ == '__main__':
main_loop()
######python求標準的一元二次方程的解###############
a,b,c= map(float,input("請輸入aX^2+bX+c=0,函數中的三個參數:(空格隔開)").split())
###使用公式b^2-4ac判定是否有解b^2-4ac####
i=b*b-4*a*c
if i0:
print("該方程無實數解!")
elif i==0:
print("該方程解為:%.2f"%((-1)*b/(2*a)))#有一個解
else:
print("該方程解為:%.2f或%.2f"%((((-1)*b+i**0.5)/(2*a)),(((-1)*b-i**0.5)/(2*a))))
該方法運用是運用公式求解,保留兩位小數,只能求實數解,供參考,有問題可追問
有不少同學學完 Python 后仍然很難將其靈活運用。我整理15個 Python 入門的小程序。在實踐中應用 Python 會有事半功倍的效果。
實現數學里的二元二次函數: f(x, y) = 2x^2 + 3y^2 + 4xy ,需要用到指數運算符 **
將一個正整數的個位數,以及除個位數外的部分分離。需要用到 模(取余數) 運算符 % ,和 整除 運算符 //
實現一個簡單的累加器,可以接受用戶輸入3個數字,并將其累加。需要用到 復合賦值運算符:+=
輸入年份,判斷是否是閏年。閏年判斷方法:能被4整除,但不能被100整除;或者能被 400 整除。需要用到 算術運算符 和 邏輯運算符
輸入一個數字,判斷基數還是偶數,需要 模 運算和 if ... else 結構
之前做過華氏度轉攝氏度,現在通過 分支結構 實現二者互轉。
輸入三個邊長度,判斷是否構成三角形。 構成三角形的條件:兩邊之和大于第三邊 。
輸入成績分數,輸出分數對應的等級。
某企業的獎金根據銷售利潤按照如下規則計算提成。輸入銷售利潤,計算獎金。
程序隨機生成一個正整數,用戶來猜,程序根據猜的大小給出相應的提示。最后,輸出用戶猜了多少次才猜中。
輸入一個正整數,判斷是否是素數。 素數定義:大于1的自然數中,只能被1和它本身整除的自然數。如:3、5、7
用程序實現石頭剪刀布 游戲 。
字典的key是姓名,value是身高,現在需要按照身高對字典重新排序。
將二元二次函數封裝在函數中,方便調用
初學python的小伙伴注意哦~不要只依賴于教程,而忽視實操的能力。不然,是很難有進步的。可以多動動手,嘗試一下,積累經驗。
本文標題:python構建二次函數 Python二次函數
文章源于:http://www.yijiale78.com/article46/hhiihg.html
成都網站建設公司_創新互聯,為您提供Google、搜索引擎優化、企業建站、靜態網站、品牌網站建設、域名注冊
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯