Skip to content

Commit b065270

Browse files
add:Numerical sign detection
Detects the sign of the numerial no matter provided in string representation or numrical.
1 parent de79841 commit b065270

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

nitkarshchourasia/determine_sign.py

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
from word2number import w2n
2+
3+
# ? word2number then w2n then .word_to_num? So, library(bunch of modules) then module then method(function)???!
4+
# return w2n.word_to_num(input_value)
5+
6+
7+
# TODO: Instead of rounding at the destination, round at the source.
8+
# Reason: As per the program need, I don't want a functionality to round or not round the number, based on the requirement, I always want to round the number.
9+
#! Will see it tomorrow.
10+
11+
12+
class DetermineSign:
13+
def __init__(self, num=None):
14+
if num is None:
15+
self.get_number()
16+
else:
17+
self.num = round(self.convert_to_float(num), 1)
18+
19+
# TODO: Word2number
20+
21+
# Need to further understand this.
22+
# ? NEED TO UNDERSTAND THIS. FOR SURETY.
23+
def convert_to_float(self, input_value):
24+
try:
25+
return float(input_value)
26+
except ValueError:
27+
try:
28+
return w2n.word_to_num(input_value)
29+
except ValueError:
30+
raise ValueError(
31+
"Invalid input. Please enter a number or a word representing a number."
32+
)
33+
34+
# Now use this in other methods.
35+
36+
def get_number(self):
37+
self.input_value = format(float(input("Enter a number: ")), ".1f")
38+
self.num = round(self.convert_to_float(self.input_value), 1)
39+
return self.num
40+
# Do I want to return the self.num?
41+
# I think I have to just store it as it is.
42+
43+
def determine_sign(self):
44+
if self.num > 0:
45+
return "Positive number"
46+
elif self.num < 0:
47+
return "Negative number"
48+
else:
49+
return "Zero"
50+
51+
def __repr__(self):
52+
return f"Number: {self.num}, Sign: {self.determine_sign()}"
53+
54+
55+
if __name__ == "__main__":
56+
number1 = DetermineSign()
57+
print(number1.determine_sign())
58+
59+
60+
# !Incomplete.

0 commit comments

Comments
 (0)