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
>>> 

沒有留言:

張貼留言

Wokwi ESP32 Simulator : ESP32 + LED +MQTT+ Node-RED Dashboard

 Wokwi ESP32 Simulator : ESP32 +  LED +MQTT+ Node-RED Dashboard  WOKWI ESP32程式 // Create an MQTT Connection // TCP Connection // import the ...