2018年1月8日 星期一

Python: Test whether a number is within 100 of 1000 or 2000

Python: Test whether a number is within 100 of 1000 or 2000

Python Basic: Exercise-17 with Solution

Write a Python program to test whether a number is within 100 of 1000 or 2000.
Python abs(x) function:
The function returns the absolute value of a number. The argument may be an integer or a floating point number. If the argument is a complex number, its magnitude is returned.
Python: abs() function
Sample Solution:-
def near_thousand(n):
      return ((abs(1000 - n) <= 100) or (abs(2000 - n) <= 100))
print(near_thousand(1000))
print(near_thousand(900))
print(near_thousand(800))   
print(near_thousand(2200))

============ RESTART: F:/Python_APSC/py-ex-basic-17.py ===========
True
True
False
False
>>> 

沒有留言:

張貼留言

Node-Red --> MQTT --> Fuxa 開源碼網頁式圖控平台

Node-Red --> MQTT --> Fuxa      FUXA(一個開源的 Web HMI / SCADA 自動化監控軟體)的專案設定檔 。 這份設定檔完整定義了 HMI 監控畫面的 後端通訊(MQTT 連線、點位標籤) 與 前端網頁圖形介面(SVG 畫布...