The question is as followed: Define a class Append. Use the parameterized constructor used to initialize the input string a. Define a method that returns the sum of each character code(ASCII value) of the given string a.
Sample test case:
CodeTantra input string
997 output
CodePudding user response:
Try something like:
s = sum(ord(x) for x in "CodeTantra")
This sums up the ASCII value of every character in the string CodeTantra
, which yields 997
.
CodePudding user response:
You can use this:
print(sum(map(ord, "CodeTantra"))) # 997
CodePudding user response:
This should work:
class Append:
def __init__(self, a: str):
self.a = a
def sum_char_codes(self):
sum = 0
for char in self.a:
sum = ord(char)
return sum
# Test the Append class
test = Append("CodeTantra")
print(test.sum_char_codes())