Skip to content

Commit febc8f6

Browse files
Improving and upgrading the addition program.\
1 parent 287b4dd commit febc8f6

File tree

1 file changed

+222
-4
lines changed

1 file changed

+222
-4
lines changed

add two no.py

Lines changed: 222 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,225 @@
1-
num1 = 1.5
2-
num2 = 6.3
1+
__author__ = "Nitkarsh Chourasia"
2+
import unittest
3+
import typing
34

45

5-
sum = num1 + num2
6+
# Docstring and document comments add.
7+
# To DRY and KISS the code.
8+
def addition(
9+
# num1: typing.Union[int, float],
10+
# num2: typing.Union[int, float]
11+
) -> str:
12+
"""A function to add two given numbers."""
613

7-
print('The sum of {0} and {1} is {2}'.format(num1, num2, sum))
14+
# If parameters are given then, add them or ask for parameters.
15+
if num1 is None:
16+
while True:
17+
try:
18+
num1 = float(input("Enter num1 value: "))
19+
break
20+
except ValueError:
21+
return "Please input numerical values only for num1."
22+
# if input is there then int or float only.
23+
# if none, then move on.
24+
25+
if num2 is None:
26+
while True:
27+
try:
28+
num2 = float(input("Enter num2 value: ")) # int conversion will cut off the data.
29+
break
30+
except ValueError:
31+
return "Please input numerical values only for num2."
32+
# if input is there then int or float only.
33+
# if none, then move on.
34+
35+
# Adding the given parameters.
36+
sum = num1 + num2
37+
38+
return f"The sum of {num1} and {num2} is: {sum}"
39+
40+
41+
print(addition(10, 11))
42+
print(addition())
43+
44+
print(__author__)
45+
46+
# class TestAdditionFunction(unittest.TestCase):
47+
#
48+
# def test_addition_with_integers(self):
49+
# result = addition(5, 10)
50+
# self.assertEqual(result, "The sum of 5 and 10 is: 15")
51+
#
52+
# def test_addition_with_floats(self):
53+
# result = addition(3.5, 4.2)
54+
# self.assertEqual(result, "The sum of 3.5 and 4.2 is: 7.7")
55+
#
56+
# def test_addition_with_invalid_input(self):
57+
# result = addition("a", "b")
58+
# self.assertEqual(result, "Please input numerical values only for num1.")
59+
#
60+
# def test_addition_with_user_input(self):
61+
# # Simulate user input for testing
62+
# user_input = ["12", "34"]
63+
# original_input = input
64+
#
65+
# def mock_input(prompt):
66+
# return user_input.pop(0)
67+
#
68+
# try:
69+
# input = mock_input
70+
# result = addition()
71+
# self.assertEqual(result, "The sum of 12.0 and 34.0 is: 46.0")
72+
# finally:
73+
# input = original_input
74+
#
75+
#
76+
# if __name__ == '__main__':
77+
# unittest.main()
78+
79+
80+
# I want to add a program to accept a number in function input.
81+
# If the following is not given then ask for the input.
82+
# Also if don't want to do anything then run the test function by uncommenting it.
83+
84+
85+
#import typing
86+
#
87+
#__author__ = "Your Name"
88+
#__version__ = "1.0"
89+
#
90+
#
91+
#def addition(
92+
# num1: typing.Union[int, float],
93+
# num2: typing.Union[int, float]
94+
#) -> str:
95+
# """A function to add two given numbers."""
96+
#
97+
# if num1 is None:
98+
# num1 = float(input("Enter num1 value: ")) # Adding the type checker.
99+
# if num2 is None:
100+
# num2 = float(input("Enter num2 value: ")) # int conversion will cut off the data.
101+
#
102+
# if not isinstance(num1, (int, float)):
103+
# return "Please input numerical values only for num1."
104+
# if not isinstance(num2, (int, float)):
105+
# return "Please input numerical values only for num2."
106+
#
107+
# # Adding the given parameters.
108+
# sum_result = num1 + num2
109+
#
110+
# return f"The sum of {num1} and {num2} is: {sum_result}"
111+
#
112+
# class TestAdditionFunction(unittest.TestCase):
113+
#
114+
# def test_addition_with_integers(self):
115+
# result = addition(5, 10)
116+
# self.assertEqual(result, "The sum of 5 and 10 is: 15")
117+
#
118+
# def test_addition_with_floats(self):
119+
# result = addition(3.5, 4.2)
120+
# self.assertEqual(result, "The sum of 3.5 and 4.2 is: 7.7")
121+
#
122+
# def test_addition_with_invalid_input(self):
123+
# result = addition("a", "b")
124+
# self.assertEqual(result, "Please input numerical values only for num1.")
125+
#
126+
# def test_addition_with_user_input(self):
127+
# # Simulate user input for testing
128+
# user_input = ["12", "34"]
129+
# original_input = input
130+
#
131+
# def mock_input(prompt):
132+
# return user_input.pop(0)
133+
#
134+
# try:
135+
# input = mock_input
136+
# result = addition(None, None)
137+
# self.assertEqual(result, "The sum of 12.0 and 34.0 is: 46.0")
138+
# finally:
139+
# input = original_input
140+
#
141+
#
142+
# if __name__ == '__main__':
143+
# unittest.main()
144+
# # See the logic in it.
145+
146+
import typing
147+
148+
__author__ = "Nitkarsh Chourasia"
149+
__version__ = "1.0"
150+
151+
152+
def addition(
153+
num1: typing.Union[int, float] = None,
154+
num2: typing.Union[int, float] = None
155+
) -> str:
156+
"""A function to add two given numbers."""
157+
158+
# If parameters are not provided, ask the user for input.
159+
if num1 is None:
160+
while True:
161+
try:
162+
num1 = float(input("Enter num1 value: "))
163+
break
164+
except ValueError:
165+
print("Please input numerical values only for num1.")
166+
167+
if num2 is None:
168+
while True:
169+
try:
170+
num2 = float(input("Enter num2 value: "))
171+
break
172+
except ValueError:
173+
print("Please input numerical values only for num2.")
174+
175+
# Adding the given parameters.
176+
sum_result = num1 + num2
177+
178+
# Returning the result.
179+
return f"The sum of {num1} and {num2} is: {sum_result}"
180+
181+
182+
# Test cases
183+
print(addition()) # This will prompt the user for input
184+
print(addition(5, 10)) # This will use the provided parameters
185+
print(addition(3.5)) # This will prompt the user for the second parameter
186+
187+
"""
188+
Requirements:
189+
# - author
190+
# - function
191+
# - DRY
192+
# - KISS
193+
# - Input type checking.
194+
- Docstring
195+
# - Commented.
196+
# - Type hinting.
197+
- Test cases.
198+
199+
---- Main motive, to take parameters if not given then ask for parameters if not given, then use default parameters.
200+
"""
201+
202+
__author__ = "Nitkarsh Chourasia"
203+
__version__ = "1.0"
204+
def addition(
205+
num1: typing.Union[int, float],
206+
num2: typing.Union[int, float]
207+
) -> str:
208+
"""A function to add two given numbers."""
209+
210+
# Checking if the given parameters are numerical or not.
211+
if not isinstance(num1, (int, float)):
212+
return "Please input numerical values only for num1."
213+
if not isinstance(num2, (int, float)):
214+
return "Please input numerical values only for num2."
215+
216+
# Adding the given parameters.
217+
sum_result = num1 + num2
218+
219+
# returning the result.
220+
return f"The sum of {num1} and {num2} is: {sum_result}"
221+
)
222+
223+
print(addition(5, 10)) # This will use the provided parameters
224+
print(addition(2, 2))
225+
print(addition(-3, -5))

0 commit comments

Comments
 (0)