iT邦幫忙

1

工程師菜鳥_宇弟的 學習筆記 [Python] 05 - [Python] [Flask] 的應用, 日期計算 及日期報錯篇

jton 2022-10-18 18:38:231154 瀏覽
  • 分享至 

  • xImage
  •  

[Python] 之旅第5天

實用練習

  • [flask] 從 URL 中獲取值並將網站上任意數的所有倍數相加
  • 日期題[python] [flask] (ex:能加減,並減少不正確的input輸入 像 “2022-02-32”, 沒有32日)

這次這個是關鍵: (用來報錯)

try:

    (主程式)

except Exception as e:
        return '''          
        
              <h1>捕捉錯誤資訊:  {}</h1>
              '''.format(diff+ str(e))

關鍵2: (把輸入的值從string 轉成 datetime data)

date = request.args['date']

date_in_system = datetime.strptime( date,  "%Y-%m-%d")

(練習1) python , Flask code 計算練習
flask 從 URL 中獲取值並將網站上的所有倍數(例如:2,到 20)相加
=>(2,4,8,16)相加
=>剛好也為 20

  • .py code 文本
# import main Flask class and request object
 
from flask import Flask, request
 
# create the Flask app
app = Flask(__name__)
 
 
@app.route('/data')
def query_example():
    # if key doesn't exist, returns None
    name = request.args.get('name')
 
    # if key doesn't exist, returns a 400, bad request error
    num1 = request.args['num1']
    num2 = request.args['num2']
 
   
 
    # take input from string to other variable變數
 
    num1_float = int(num1)
    num2_float = int(num2)
 
    multiply = num1_float*num2_float
 
    i=1
    sum2=0
 
    while i< num1_float:
        if i % num2_float ==0:
            sum2+=i
        i+=1
    aaa =sum2
    return '''
              <h1>The name is: {}</h1>
              <h1>The num1  is: {}</h1>
              <h1>The num2  is: {}</h1>  
              <h1>The multiply is: {}</h1>
              <h1>The sum2  is: {}</h1>
              '''.format(name, num1_float,num2_float , multiply ,aaa)
             
 
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

flask 從 URL 中獲取值並將網站上的所有倍數(例如:4,最多 50)=>(312)相加 網站
https://stackoverflow.com/questions/73768467/solved-in-4th-code-flask-taking-value-from-url-and-sum-all-the-multiple-numbe

成果會長這樣喔:
https://ithelp.ithome.com.tw/upload/images/20221018/20153034AtdSrBIU5U.png


(練習2) 日期題[python] [flask] (ex:能加減,並減少不正確的input輸入 像 “2022-02-32”, 沒有32日)
用日期來做計算喔

以下的代碼 能抓日子,做運算, operation [add] [del], 跟報錯

import datetime
from datetime import datetime, timedelta
from flask import Flask, request
from datetime import date,  datetime, time ,timedelta
app = Flask(__name__)
 
@app.route('/data')
 
def query_example():
   
    try:
        date = request.args['date']
        diff = request.args['diff']
        op = request.args.get('op')
       
        diff_int = int(diff)
       
        date_in_system = datetime.strptime( date,  "%Y-%m-%d")
   
        new_date_add = date_in_system + timedelta(days = diff_int )
        new_date_minus = date_in_system - timedelta(days = diff_int )
 
        final_date = 0
 
        if op == "add":
            final_date = date_in_system + timedelta(days = diff_int )
 
        if op == "del":
            final_date = date_in_system - timedelta(days = diff_int )
 
   
    except Exception as e:
        return '''          
       
              <h1>捕捉錯誤資訊:  {}</h1>
              '''.format(diff+ str(e))
  
    return '''          
              <h1>The diff is: {}</h1>
              <h1>The date is: {}</h1>
              <h1>The op is: {}</h1>
             
              <h1>The final_date is: {}'''.format(diff, date_in_system , op, final_date)
             
   
if __name__ == '__main__':
    # run app in debug mode on port 5000
    app.run(debug=True, port=5000)

成果會長這樣喔:

  1. URL輸入日期 13月 會給用戶報錯
    https://ithelp.ithome.com.tw/upload/images/20221018/20153034QKqwUywwYG.png

2.正確的日期輸入,會幫用戶計算想要的天數差(範例為 6天前 跟6天后)
https://ithelp.ithome.com.tw/upload/images/20221018/20153034jVrr9gMIeM.png


其他貼文~~
(變強,就從小小的累積開始)

[Python] 之旅第1天 - python環境
https://ithelp.ithome.com.tw/articles/10296280

[Python] 之旅第2天-用 [Python] 跟 [Flask] 為基礎,把 URL input 抓出來
https://ithelp.ithome.com.tw/articles/10296290

[Python] 之旅第3天- 用 [Python] 跟 [Flask]
https://ithelp.ithome.com.tw/articles/10296965

[Python] 之旅第4天- [Python] [Flask] 的應用
https://ithelp.ithome.com.tw/articles/10303948

[Python] 之旅第5天- [Python] [Flask] 的應用, 日期計算 及日期報錯篇
https://ithelp.ithome.com.tw/articles/10309202

[Python] 之旅第6天- [Python] [Flask] 的應用, 比較有小數點圓的面積大小,簡易請假系統製作
https://ithelp.ithome.com.tw/articles/10309352

[Python] 之旅第7天- [Python] [regular expression] 的應用,找特定資料,或限制輸入的格式
https://ithelp.ithome.com.tw/articles/10309371

[Python] 之旅第8天 - [Python] [regular expression] 的應用, 甚麼是 Json
https://ithelp.ithome.com.tw/articles/10309422

[Python] 之旅第9天 - [Python] [Json] 進階說明
https://ithelp.ithome.com.tw/articles/10309620


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言