Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- def calculate_numbers_in_braces(string):
- """
- Calculates the sum of all numbers within curly braces {} in a given string.
- Args:
- string: The input string.
- Returns:
- The sum of all numbers found within curly braces.
- """
- numbers_str = re.findall(r'\{([^}]+)\}', string)
- total = 0
- for num_str in numbers_str:
- try:
- numbers = [int(x) for x in num_str.split(',')]
- total += sum(numbers)
- except ValueError:
- print(f"Error: Invalid number format in '{num_str}'")
- return total
- # Example usage:
- string = "{1,2,3}"
- result = calculate_numbers_in_braces(string)
- print(result) # Output: 6
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement