2018年1月8日 星期一

Python: Get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference

Python: Get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference

Python Basic: Exercise-16 with Solution

Write a Python program to get the difference between a given number and 17, if the number is greater than 17 return double the absolute difference.
Python if-else syntax:
if condition :
indentedStatementBlockForTrueCondition
else:
indentedStatementBlockForFalseCondition
These statement blocks can have any number of statements, and can include about any kind of statement.
Sample Solution:-
def difference(n):
    if n <= 17:
        return 17 - n
    else:
        return (n - 17) * 2 

print(difference(22))
print(difference(14))

========== RESTART: F:/Python_APSC/py-ex-basic-16.py ===========
10
3
>>> 

沒有留言:

張貼留言

RFID TI 培訓影片系列

RFID TI 培訓影片系列  https://www.ti.com/zh-tw/video/series/rfid.html 培訓影片系列 RFID 隨著創新技術日益發展,RFID 和 RF 術語越來越容易讓人混淆。本訓練系列詳細介紹了使用案例、權衡技術優缺點,讓您清楚知道該選...