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
Question
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
Solution
To write a program that imports the function add(a, b) from the file add_0.py and prints the result of the addition 1 + 2 = 3, follow these steps:
-
Assign the value
1to a variable calleda:a = 1 -
Assign the value
2to a variable calledb:b = 2 -
Import the
addfunction from theadd_0.pyfile:from add_0 import add -
Call the
addfunction with the variablesaandbas arguments and assign the result to a variable calledresult:result = add(a, b) -
Print the result using the
printfunction with string formatting:print("{} + {} = {}".format(a, b, result)) -
The program should output
<a value> + <b value> = <add(a, b) value>followed by a new line.
Note: Make sure not to execute the code when importing it using __import__.
Similar Questions
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
Write a function name add that takes two parameter a and b, then return the output of a + b (Do not use any other variable! You do not need to run it. Only write the code about how you define it.)
ct answerWhat will be the output of the following Python code snippet?print('Addition of {0:x}, {1:o} gives {2:b}'.format(1, 5, 6))
What is the output of the add() function calldef add(a, b): return a+5, b+5result = add(3, 2)print(result)158(8, 7)Syntax Error
InstructionsAssign the integer 10 to a variable named variable_1.Assign the float 2.5 to a variable named variable_2.Update the value of variable_1 by adding the float 6.5 to its current value. You can use the += operator.Update the value of variable_2 by multiplying its current value by the integer 2. You can use the *= operator.Display variable_1 and variable_2 using print().
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.