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

沒有留言:

張貼留言

Python Modbus 控制 ADAM-6050 18-ch Isolated Digital I/O Module

Python Modbus 控制  ADAM-6050 18-ch Isolated Digital I/O Module import tkinter as tk from tkinter import messagebox from pymodbus.client impor...