Files
python-test/test16.py
T

49 lines
1.3 KiB
Python

class BoundedInt():
def __init__(self, i, min, max):
self.i = i
self.min = min
self.max = max
def __index__(self):
return self.i
def __add__(self, other):
if isinstance(other, BoundedInt):
result = self.i + other.i
elif isinstance(other, str):
score = sentiment(other)
result = self.i + score
else:
result = self.i + other
if result > self.max:
raise ValueError("Result exceeds max value")
return BoundedInt(result, self.min, self.max)
def __sub__(self, other):
if isinstance(other, BoundedInt):
result = self.i - other.i
elif isinstance(other, str):
score = sentiment(other)
result = self.i - score
else:
result = self.i - other
if result < self.min:
raise ValueError("Result exceeds min value")
return BoundedInt(result, self.min, self.max)
def sentiment(emoji):
sentiment_scores = {
"😀": 8.9,
"😢": -6.2,
"😡": -9.5,
"😍": 9.8,
}
return sentiment_scores.get(emoji, 0.0)
if __name__ == '__main__':
b = BoundedInt(3, min = 0, max = 8)
b -= "😢"
#a = [1,2,3,4,5,6,7,8,9,10]
#print(f"{a[b]=}")