From febc8f600b8905b33e6250ec41185ed3a0c1abdc Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:51:51 +0530 Subject: [PATCH 1/9] Improving and upgrading the addition program.\ --- add two no.py | 226 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 222 insertions(+), 4 deletions(-) diff --git a/add two no.py b/add two no.py index d1b6fd9e455..5ccf39f37f7 100644 --- a/add two no.py +++ b/add two no.py @@ -1,7 +1,225 @@ -num1 = 1.5 -num2 = 6.3 +__author__ = "Nitkarsh Chourasia" +import unittest +import typing -sum = num1 + num2 +# Docstring and document comments add. +# To DRY and KISS the code. +def addition( + # num1: typing.Union[int, float], + # num2: typing.Union[int, float] +) -> str: + """A function to add two given numbers.""" -print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) + # If parameters are given then, add them or ask for parameters. + if num1 is None: + while True: + try: + num1 = float(input("Enter num1 value: ")) + break + except ValueError: + return "Please input numerical values only for num1." + # if input is there then int or float only. + # if none, then move on. + + if num2 is None: + while True: + try: + num2 = float(input("Enter num2 value: ")) # int conversion will cut off the data. + break + except ValueError: + return "Please input numerical values only for num2." + # if input is there then int or float only. + # if none, then move on. + + # Adding the given parameters. + sum = num1 + num2 + + return f"The sum of {num1} and {num2} is: {sum}" + + +print(addition(10, 11)) +print(addition()) + +print(__author__) + +# class TestAdditionFunction(unittest.TestCase): +# +# def test_addition_with_integers(self): +# result = addition(5, 10) +# self.assertEqual(result, "The sum of 5 and 10 is: 15") +# +# def test_addition_with_floats(self): +# result = addition(3.5, 4.2) +# self.assertEqual(result, "The sum of 3.5 and 4.2 is: 7.7") +# +# def test_addition_with_invalid_input(self): +# result = addition("a", "b") +# self.assertEqual(result, "Please input numerical values only for num1.") +# +# def test_addition_with_user_input(self): +# # Simulate user input for testing +# user_input = ["12", "34"] +# original_input = input +# +# def mock_input(prompt): +# return user_input.pop(0) +# +# try: +# input = mock_input +# result = addition() +# self.assertEqual(result, "The sum of 12.0 and 34.0 is: 46.0") +# finally: +# input = original_input +# +# +# if __name__ == '__main__': +# unittest.main() + + +# I want to add a program to accept a number in function input. +# If the following is not given then ask for the input. +# Also if don't want to do anything then run the test function by uncommenting it. + + +#import typing +# +#__author__ = "Your Name" +#__version__ = "1.0" +# +# +#def addition( +# num1: typing.Union[int, float], +# num2: typing.Union[int, float] +#) -> str: +# """A function to add two given numbers.""" +# +# if num1 is None: +# num1 = float(input("Enter num1 value: ")) # Adding the type checker. +# if num2 is None: +# num2 = float(input("Enter num2 value: ")) # int conversion will cut off the data. +# +# if not isinstance(num1, (int, float)): +# return "Please input numerical values only for num1." +# if not isinstance(num2, (int, float)): +# return "Please input numerical values only for num2." +# +# # Adding the given parameters. +# sum_result = num1 + num2 +# +# return f"The sum of {num1} and {num2} is: {sum_result}" +# +# class TestAdditionFunction(unittest.TestCase): +# +# def test_addition_with_integers(self): +# result = addition(5, 10) +# self.assertEqual(result, "The sum of 5 and 10 is: 15") +# +# def test_addition_with_floats(self): +# result = addition(3.5, 4.2) +# self.assertEqual(result, "The sum of 3.5 and 4.2 is: 7.7") +# +# def test_addition_with_invalid_input(self): +# result = addition("a", "b") +# self.assertEqual(result, "Please input numerical values only for num1.") +# +# def test_addition_with_user_input(self): +# # Simulate user input for testing +# user_input = ["12", "34"] +# original_input = input +# +# def mock_input(prompt): +# return user_input.pop(0) +# +# try: +# input = mock_input +# result = addition(None, None) +# self.assertEqual(result, "The sum of 12.0 and 34.0 is: 46.0") +# finally: +# input = original_input +# +# +# if __name__ == '__main__': +# unittest.main() +# # See the logic in it. + +import typing + +__author__ = "Nitkarsh Chourasia" +__version__ = "1.0" + + +def addition( + num1: typing.Union[int, float] = None, + num2: typing.Union[int, float] = None +) -> str: + """A function to add two given numbers.""" + + # If parameters are not provided, ask the user for input. + if num1 is None: + while True: + try: + num1 = float(input("Enter num1 value: ")) + break + except ValueError: + print("Please input numerical values only for num1.") + + if num2 is None: + while True: + try: + num2 = float(input("Enter num2 value: ")) + break + except ValueError: + print("Please input numerical values only for num2.") + + # Adding the given parameters. + sum_result = num1 + num2 + + # Returning the result. + return f"The sum of {num1} and {num2} is: {sum_result}" + + +# Test cases +print(addition()) # This will prompt the user for input +print(addition(5, 10)) # This will use the provided parameters +print(addition(3.5)) # This will prompt the user for the second parameter + +""" +Requirements: +# - author +# - function +# - DRY +# - KISS +# - Input type checking. +- Docstring +# - Commented. +# - Type hinting. +- Test cases. + +---- Main motive, to take parameters if not given then ask for parameters if not given, then use default parameters. +""" + +__author__ = "Nitkarsh Chourasia" +__version__ = "1.0" +def addition( + num1: typing.Union[int, float], + num2: typing.Union[int, float] +) -> str: + """A function to add two given numbers.""" + + # Checking if the given parameters are numerical or not. + if not isinstance(num1, (int, float)): + return "Please input numerical values only for num1." + if not isinstance(num2, (int, float)): + return "Please input numerical values only for num2." + + # Adding the given parameters. + sum_result = num1 + num2 + + # returning the result. + return f"The sum of {num1} and {num2} is: {sum_result}" +) + +print(addition(5, 10)) # This will use the provided parameters +print(addition(2, 2)) +print(addition(-3, -5)) From 4156ff24b06afad87d6bea812a541e821ef44b6f Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:54:21 +0530 Subject: [PATCH 2/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- add two number.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 add two number.py diff --git a/add two number.py b/add two number.py deleted file mode 100644 index 8fbba127e70..00000000000 --- a/add two number.py +++ /dev/null @@ -1,10 +0,0 @@ -# This program adds two numbers - -num1 = 1.5 -num2 = 6.3 - -# Add two numbers -sum = num1 + num2 - -# Display the sum -print('The sum of {0} and {1} is {2}'.format(num1, num2, sum)) From 0d1979790e2c47d4c9aeffe40c67e564a8c4fae8 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:54:41 +0530 Subject: [PATCH 3/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- Add two numbers.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 Add two numbers.py diff --git a/Add two numbers.py b/Add two numbers.py deleted file mode 100644 index 7ad18d74ac8..00000000000 --- a/Add two numbers.py +++ /dev/null @@ -1,10 +0,0 @@ -# User pick two numbers to sum - -num1 = float(input("Number 1:")) -num2 = float(input("Number 2:")) - -# Add two numbers -sum = num1 + num2 - -# Display the sum -print(f"The sum of {num1} and {num2} is {sum}") From fec57c39c76a007a930b8eaaf143f8141f873704 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:54:53 +0530 Subject: [PATCH 4/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- add 2 number.py | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 add 2 number.py diff --git a/add 2 number.py b/add 2 number.py deleted file mode 100644 index e10b54423d5..00000000000 --- a/add 2 number.py +++ /dev/null @@ -1,3 +0,0 @@ -num1=int(input("Enter the First Number : ")) -num2=int(input("Enter the Second Number : ")) -print("Sum:",num1 + num2) From bddbd6942a8eef9c7957ae97d4b1d5a085c6728f Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:55:06 +0530 Subject: [PATCH 5/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- add 2 numbers.py | 9 --------- 1 file changed, 9 deletions(-) delete mode 100644 add 2 numbers.py diff --git a/add 2 numbers.py b/add 2 numbers.py deleted file mode 100644 index cf300757152..00000000000 --- a/add 2 numbers.py +++ /dev/null @@ -1,9 +0,0 @@ -# This program adds two numbers -# Works with Python 3.6 version and above. - -# Set the values. -num1 = 1.5 -num2 = 6.3 - -# Display the sum. -print(f'The sum of {num1} and {num2} is {num1 + num2}.') From 27b792f74c41e9cfb2d81ff3e20bbd98b14ad99a Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:55:17 +0530 Subject: [PATCH 6/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- add_2_nums.py | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 add_2_nums.py diff --git a/add_2_nums.py b/add_2_nums.py deleted file mode 100644 index 0b7bf5b84db..00000000000 --- a/add_2_nums.py +++ /dev/null @@ -1,5 +0,0 @@ -# for item in # this python program asks uses for 2 number and returns their sum: - -a = int(input("enter first Number: ")) -b = int(input("enter second Number: ")) -print("sum is:", a+b) From b75e8e58ab765e7038ae254eb6fba475c2e9c58b Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:55:36 +0530 Subject: [PATCH 7/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- Addtion of two numbers.py | 12 ------------ 1 file changed, 12 deletions(-) delete mode 100644 Addtion of two numbers.py diff --git a/Addtion of two numbers.py b/Addtion of two numbers.py deleted file mode 100644 index ffe570b7002..00000000000 --- a/Addtion of two numbers.py +++ /dev/null @@ -1,12 +0,0 @@ -# Python3 program to add two numbers - -number1 = input("First number: ") -number2 = input("\nSecond number: ") - -# Adding two numbers -# User might also enter float numbers -sum = float(number1) + float(number2) - -# Display the sum -# will print value in float -print("The sum of {0} and {1} is {2}".format(number1, number2, sum)) From c122608105f623ec7da70c4f89542aa45b6e9431 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 13:55:54 +0530 Subject: [PATCH 8/9] Duplicate file as of previous commited, pollutes the repository, serves no extra function, program to serve the same function already present. --- addtwonumber.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 addtwonumber.py diff --git a/addtwonumber.py b/addtwonumber.py deleted file mode 100644 index a290862ecad..00000000000 --- a/addtwonumber.py +++ /dev/null @@ -1,7 +0,0 @@ -#Python Program to Add Two Numbers -a = int(input("enter first number: ")) -b = int(input("enter second number: ")) - -sum = a + b - -print("sum:", sum) From 05ac4be74175b71fc511347937a76817e8e7e935 Mon Sep 17 00:00:00 2001 From: Nitkarsh Chourasia Date: Thu, 10 Aug 2023 14:02:23 +0530 Subject: [PATCH 9/9] Update and rename add two no.py to add_two_nums.py Cleaned and Optimized the program for better readability and execution. --- add two no.py | 225 ------------------------------------------------ add_two_nums.py | 24 ++++++ 2 files changed, 24 insertions(+), 225 deletions(-) delete mode 100644 add two no.py create mode 100644 add_two_nums.py diff --git a/add two no.py b/add two no.py deleted file mode 100644 index 5ccf39f37f7..00000000000 --- a/add two no.py +++ /dev/null @@ -1,225 +0,0 @@ -__author__ = "Nitkarsh Chourasia" -import unittest -import typing - - -# Docstring and document comments add. -# To DRY and KISS the code. -def addition( - # num1: typing.Union[int, float], - # num2: typing.Union[int, float] -) -> str: - """A function to add two given numbers.""" - - # If parameters are given then, add them or ask for parameters. - if num1 is None: - while True: - try: - num1 = float(input("Enter num1 value: ")) - break - except ValueError: - return "Please input numerical values only for num1." - # if input is there then int or float only. - # if none, then move on. - - if num2 is None: - while True: - try: - num2 = float(input("Enter num2 value: ")) # int conversion will cut off the data. - break - except ValueError: - return "Please input numerical values only for num2." - # if input is there then int or float only. - # if none, then move on. - - # Adding the given parameters. - sum = num1 + num2 - - return f"The sum of {num1} and {num2} is: {sum}" - - -print(addition(10, 11)) -print(addition()) - -print(__author__) - -# class TestAdditionFunction(unittest.TestCase): -# -# def test_addition_with_integers(self): -# result = addition(5, 10) -# self.assertEqual(result, "The sum of 5 and 10 is: 15") -# -# def test_addition_with_floats(self): -# result = addition(3.5, 4.2) -# self.assertEqual(result, "The sum of 3.5 and 4.2 is: 7.7") -# -# def test_addition_with_invalid_input(self): -# result = addition("a", "b") -# self.assertEqual(result, "Please input numerical values only for num1.") -# -# def test_addition_with_user_input(self): -# # Simulate user input for testing -# user_input = ["12", "34"] -# original_input = input -# -# def mock_input(prompt): -# return user_input.pop(0) -# -# try: -# input = mock_input -# result = addition() -# self.assertEqual(result, "The sum of 12.0 and 34.0 is: 46.0") -# finally: -# input = original_input -# -# -# if __name__ == '__main__': -# unittest.main() - - -# I want to add a program to accept a number in function input. -# If the following is not given then ask for the input. -# Also if don't want to do anything then run the test function by uncommenting it. - - -#import typing -# -#__author__ = "Your Name" -#__version__ = "1.0" -# -# -#def addition( -# num1: typing.Union[int, float], -# num2: typing.Union[int, float] -#) -> str: -# """A function to add two given numbers.""" -# -# if num1 is None: -# num1 = float(input("Enter num1 value: ")) # Adding the type checker. -# if num2 is None: -# num2 = float(input("Enter num2 value: ")) # int conversion will cut off the data. -# -# if not isinstance(num1, (int, float)): -# return "Please input numerical values only for num1." -# if not isinstance(num2, (int, float)): -# return "Please input numerical values only for num2." -# -# # Adding the given parameters. -# sum_result = num1 + num2 -# -# return f"The sum of {num1} and {num2} is: {sum_result}" -# -# class TestAdditionFunction(unittest.TestCase): -# -# def test_addition_with_integers(self): -# result = addition(5, 10) -# self.assertEqual(result, "The sum of 5 and 10 is: 15") -# -# def test_addition_with_floats(self): -# result = addition(3.5, 4.2) -# self.assertEqual(result, "The sum of 3.5 and 4.2 is: 7.7") -# -# def test_addition_with_invalid_input(self): -# result = addition("a", "b") -# self.assertEqual(result, "Please input numerical values only for num1.") -# -# def test_addition_with_user_input(self): -# # Simulate user input for testing -# user_input = ["12", "34"] -# original_input = input -# -# def mock_input(prompt): -# return user_input.pop(0) -# -# try: -# input = mock_input -# result = addition(None, None) -# self.assertEqual(result, "The sum of 12.0 and 34.0 is: 46.0") -# finally: -# input = original_input -# -# -# if __name__ == '__main__': -# unittest.main() -# # See the logic in it. - -import typing - -__author__ = "Nitkarsh Chourasia" -__version__ = "1.0" - - -def addition( - num1: typing.Union[int, float] = None, - num2: typing.Union[int, float] = None -) -> str: - """A function to add two given numbers.""" - - # If parameters are not provided, ask the user for input. - if num1 is None: - while True: - try: - num1 = float(input("Enter num1 value: ")) - break - except ValueError: - print("Please input numerical values only for num1.") - - if num2 is None: - while True: - try: - num2 = float(input("Enter num2 value: ")) - break - except ValueError: - print("Please input numerical values only for num2.") - - # Adding the given parameters. - sum_result = num1 + num2 - - # Returning the result. - return f"The sum of {num1} and {num2} is: {sum_result}" - - -# Test cases -print(addition()) # This will prompt the user for input -print(addition(5, 10)) # This will use the provided parameters -print(addition(3.5)) # This will prompt the user for the second parameter - -""" -Requirements: -# - author -# - function -# - DRY -# - KISS -# - Input type checking. -- Docstring -# - Commented. -# - Type hinting. -- Test cases. - ----- Main motive, to take parameters if not given then ask for parameters if not given, then use default parameters. -""" - -__author__ = "Nitkarsh Chourasia" -__version__ = "1.0" -def addition( - num1: typing.Union[int, float], - num2: typing.Union[int, float] -) -> str: - """A function to add two given numbers.""" - - # Checking if the given parameters are numerical or not. - if not isinstance(num1, (int, float)): - return "Please input numerical values only for num1." - if not isinstance(num2, (int, float)): - return "Please input numerical values only for num2." - - # Adding the given parameters. - sum_result = num1 + num2 - - # returning the result. - return f"The sum of {num1} and {num2} is: {sum_result}" -) - -print(addition(5, 10)) # This will use the provided parameters -print(addition(2, 2)) -print(addition(-3, -5)) diff --git a/add_two_nums.py b/add_two_nums.py new file mode 100644 index 00000000000..f68631f2ea1 --- /dev/null +++ b/add_two_nums.py @@ -0,0 +1,24 @@ +__author__ = "Nitkarsh Chourasia" +__version__ = "1.0" +def addition( + num1: typing.Union[int, float], + num2: typing.Union[int, float] +) -> str: + """A function to add two given numbers.""" + + # Checking if the given parameters are numerical or not. + if not isinstance(num1, (int, float)): + return "Please input numerical values only for num1." + if not isinstance(num2, (int, float)): + return "Please input numerical values only for num2." + + # Adding the given parameters. + sum_result = num1 + num2 + + # returning the result. + return f"The sum of {num1} and {num2} is: {sum_result}" +) + +print(addition(5, 10)) # This will use the provided parameters +print(addition(2, 2)) +print(addition(-3, -5))