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

沒有留言:

張貼留言