Knowee
Questions
Features
Study Tools

Write a function that adds 2 integers.Prototype: def add_integer(a, b=98):a and b must be integers or floats, otherwise raise a TypeError exception with the message a must be an integer or b must be an integera and b must be first casted to integers if they are floatReturns an integer: the addition of a and bYou are not allowed to import any moduleguillaume@ubuntu:~/0x07$ cat 0-main.py#!/usr/bin/python3add_integer = __import__('0-add_integer').add_integerprint(add_integer(1, 2))print(add_integer(100, -2))print(add_integer(2))print(add_integer(100.3, -2))try: print(add_integer(4, "School"))except Exception as e: print(e)try: print(add_integer(None))except Exception as e: print(e)guillaume@ubuntu:~/0x07$ ./0-main.py39810098b must be an integera must be an integerguillaume@ubuntu:~/0x07$ python3 -m doctest -v ./tests/0-add_integer.txt | tail -29 passed and 0 failed.Test passed.guillaume@ubuntu:~/0x07$ python3 -c 'print(__import__("0-add_integer").__doc__)' | wc -l5guillaume@ubuntu:~/0x07$ python3 -c 'print(__import__("0-add_integer").add_integer.__doc__)' | wc -l3guillaume@ubuntu:~/0x07$ Repo:GitHub repository: alx-higher_level_programmingDirectory: 0x07-python-test_driven_developmentFile: 0-add_integer.py, tests/0-add_integer.txt

Question

Write a function that adds 2 integers.Prototype: def add_integer(a, b=98):a and b must be integers or floats, otherwise raise a TypeError exception with the message a must be an integer or b must be an integera and b must be first casted to integers if they are floatReturns an integer: the addition of a and bYou are not allowed to import any moduleguillaume@ubuntu:~/0x07 cat 0-main.py#!/usr/bin/python3add_integer = __import__('0-add_integer').add_integerprint(add_integer(1, 2))print(add_integer(100, -2))print(add_integer(2))print(add_integer(100.3, -2))try: print(add_integer(4, "School"))except Exception as e: print(e)try: print(add_integer(None))except Exception as e: print(e)guillaume@ubuntu:~/0x07 ./0-main.py39810098b must be an integera must be an integerguillaume@ubuntu:~/0x07python3mdoctestv./tests/0addinteger.txttail29passedand0failed.Testpassed.guillaume@ubuntu: /0x07 python3 -m doctest -v ./tests/0-add_integer.txt | tail -29 passed and 0 failed.Test passed.guillaume@ubuntu:~/0x07 python3 -c 'print(import("0-add_integer").doc)' | wc -l5guillaume@ubuntu:~/0x07 python3 -c 'print(__import__("0-add_integer").add_integer.__doc__)' | wc -l3guillaume@ubuntu:~/0x07 Repo:GitHub repository: alx-higher_level_programmingDirectory: 0x07-python-test_driven_developmentFile: 0-add_integer.py, tests/0-add_integer.txt

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Sure, here is the Python function that meets your requirements:

def add_integer(a, b=98):
    """
    This function adds two integers or floats.

    If the arguments are not integers or floats, it raises a TypeError.

    If the arguments are floats, they are first casted to integers.

    Returns the sum of the two numbers.
    """
    if not isinstance(a, (int, float)):
        raise TypeError("a must be an integer")
    if not isinstance(b, (int, float)):
        raise TypeError("b must be an integer")
    a = int(a)
    b = int(b)
    return a + b

This function first checks if the inputs a and b are either integers or floats. If not, it raises a TypeError. Then, it casts a and b to integers (if they are not already), and finally returns the sum of a and b.

This problem has been solved

Similar Questions

Write a program that imports the function def add(a, b): from the file add_0.py and prints the result of the addition 1 + 2 = 3You have to use print function with string format to display integersYou have to assign:the value 1 to a variable called athe value 2 to a variable called band use those two variables as arguments when calling the functions add and printa and b must be defined in 2 different lines: a = 1 and another b = 2Your program should print: <a value> + <b value> = <add(a, b) value> followed with a new lineYou can only use the word add_0 once in your codeYou are not allowed to use * for importing or __import__Your code should not be executed when imported - by using __import__, like the example below

0. Basic annotations - addmandatoryWrite a type-annotated function add that takes a float a and a float b as arguments and returns their sum as a float.bob@dylan:~$ cat 0-main.py#!/usr/bin/env python3add = __import__('0-add').addprint(add(1.11, 2.22) == 1.11 + 2.22)print(add.__annotations__)bob@dylan:~$ ./0-main.pyTrue{'a': <class 'float'>, 'b': <class 'float'>, 'return': <class 'float'>}

Add two numbersYou have been provided with some code. The code should take in two numbers from the user, add them together, and then display the result to the user. However, the code has bugs!Debug this code!Here are some examples of how the program should work:Example 1Enter a number: 32Enter another number: 64The sum of your two numbers is: 96.00Example 2Enter a number: 1234.5678Enter another number: 9999.99The sum of your two numbers is: 11234.56Example 3Enter a number: 23Enter another number: -532.824The sum of your two numbers is: -509.82

class Solution: def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode: dummyHead = ListNode(0) tail = dummyHead carry = 0 while l1 is not None or l2 is not None or carry != 0: digit1 = l1.val if l1 is not None else 0 digit2 = l2.val if l2 is not None else 0 sum = digit1 + digit2 + carry digit = sum % 10 carry = sum // 10 newNode = ListNode(digit) tail.next = newNode tail = tail.next l1 = l1.next if l1 is not None else None l2 = l2.next if l2 is not None else None result = dummyHead.next dummyHead.next = None return result

Create a class Calculator with overloaded methods for addition:A method add(int a, int b) to add two integers.A method add(float a, float b) to add two floating-point numbers.A method add(const std::string& a, const std::string& b) to add two strings representing numbers.A method add(int a, int b, int c) to add three integers.Implement a main program that:Creates an instance of Calculator.Calls the overloaded add methods with different argument types and counts.Displays the results for each addition operation.

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.