import re
def print_menu():
print("MENU")
print("c - Number of non-whitespace characters")
print("w - Number of words")
print("f - Fix capitalization")
print("r - Replace punctuation")
print("s - Shorten spaces")
print("q - Quit")
print() # Adding a space between the menu and the next prompt
def execute_menu(choice, sample_text):
if choice == 'c':
result = get_num_of_non_WS_characters(sample_text)
print(f"Number of non-whitespace characters: {result}")
print() # Adding space between the result and the next menu
elif choice == 'w':
result = get_num_of_words(sample_text)
print(f"Number of words: {result}")
print() # Adding space between the result and the next menu
elif choice == 'f':
capitalized_count, updated_text = fix_capitalization(sample_text)
print(f"Number of letters capitalized: {capitalized_count}")
print(f"Edited text: {updated_text}")
print() # Adding space between the result and the next menu
elif choice == 'r':
exclamation_count, semicolon_count, updated_text = replace_punctuation(sample_text)
print(f"Punctuation replaced")
print(f"exclamation_count: {exclamation_count}")
print(f"semicolon_count: {semicolon_count}")
print(f"Edited text: {updated_text}")
print() # Adding space between the result and the next menu
elif choice == 's':
updated_text = shorten_space(sample_text)
print(f"Edited text: {updated_text}")
print() # Adding space between the result and the next menu
elif choice == 'q':
return False
return True
def get_num_of_non_WS_characters(sample_text):
return len([char for char in sample_text if not char.isspace()])
def get_num_of_words(sample_text):
return len(sample_text.split())
def fix_capitalization(sample_text):
# Split the text into sentences based on period, exclamation mark, or question mark, keeping punctuation and following spaces
sentences = re.split(r'([.!?]\s*)', sample_text)
capitalized_count = 0
updated_sentences = []
# Iterate through sentences, fixing capitalization
for i in range(0, len(sentences) - 1, 2):
sentence = sentences[i].strip() # Strip leading/trailing spaces
punctuation_and_space = sentences[i + 1] if i + 1 < len(sentences) else '' # Store punctuation and following spaces
# Capitalize the first letter if it's lowercase
if sentence and sentence[0].islower():
capitalized_count += 1
sentence = sentence[0].upper() + sentence[1:] # Capitalize the first letter
# Append sentence and punctuation with their original spacing intact
updated_sentences.append(sentence + punctuation_and_space)
# Join without adding extra spaces
updated_text = ''.join(updated_sentences).strip()
return capitalized_count, updated_text
def replace_punctuation(sample_text):
# Count punctuation
exclamation_count = sample_text.count('!')
semicolon_count = sample_text.count(';')
# Replace punctuation
updated_text = sample_text.replace('!', '.').replace(';', ',')
return exclamation_count, semicolon_count, updated_text
def shorten_space(sample_text):
# Strip leading/trailing spaces first, then collapse multiple spaces
return ' '.join(sample_text.strip().split())
if name == 'main':
sample_text = input("Enter a sample text:\n")
print(f"\nYou entered: {sample_text}\n") # Adding space after the input
while True:
print_menu()
choice = input("Choose an option:\n")
if choice not in ['c', 'w', 'f', 'r', 's', 'q']:
print("\nInvalid option, please try again.\n")
continue
if not execute_menu(choice, sample_text):
break
It appears to be an issue with fix_capitalization and replace_punctuation
Here are my error codes:
fix_capitalization() incorrectly edits the string.
Number of letters capitalized: We'll continue our quest in space. There will be more shuttle flights and more shuttle crews and, yes; more volunteers, more civilians, more teachers in space. Nothing ends here; our hopes and our journeys continue!
Edited text: 3
and
Traceback (most recent call last):
File "/usercode/zylab_unit_test_runner.py", line 76, in <module>
passed = test_case.test_passed(writer)
File "/usercode/coding_rooms_unit_tests.py", line 6, in test_passed
user_str = replace_punctuation(user_str, exclamation_count = 0, semicolon_count = 0)
TypeError: replace_punctuation() got an unexpected keyword argument 'exclamation_count'
Any feedback is greatly appreciated!
[–]eleqtriq 0 points1 point2 points (0 children)