2018年1月8日 星期一

Python: Add two objects if both objects are an integer type

Python: Add two objects if both objects are an integer type

Python Basic: Exercise-36 with Solution

Write a Python program to add two objects if both objects are an integer type.
Sample Solution:-
def add_numbers(a, b):
    if not (isinstance(a, int) and isinstance(b, int)):
         raise TypeError("Inputs must be integers")
    return a + b

print(add_numbers(10, 20))

print(add_numbers(1234, 8765))

========= RESTART: F:/Python_APSC/py-ex-basic-36.py ===========
30
9999
>>> 

沒有留言:

張貼留言

OPEN DATA AQI 空氣品質監測網 + NODE-RED

OPEN DATA AQI 空氣品質監測網 + NODE-RED  首頁 - 空氣品質監測網  https://airtw.moenv.gov.tw/ https://data.gov.tw/datasets/search?p=1&size=10&s=_score...