Поменять числа в строке на слова

Функция, которая принимает строку и будет возвращать строку, в которой:

  • все знаки препинания убраны.
  • все гласные будут заглавные,
  • все согласные прописные,
  • а цифры изменены на слова.
text = '''SMS messaging was used for the first time on 3 December 1992,[8]
when Neil Papworth, a 22-year-old test engineer for Sema Group in the UK[9] (now Airwide 
Solutions),[10] used a personal computer to send the text message 'Merry Christmas'
via the Vodafone network to the phone of Richard Jarvis,[11][12] who was at a party
in Newbury, Berkshire, which had been organized to celebrate the event. Modern SMS
text messaging is usually messaging from one mobile phone to another. Finnish 
Radiolinja became the first network to offer a commercial person-to-person SMS text 
messaging service in 1994.'''
import string

myNewText = text



def modified_text_puncuation(myNewText):
    return myNewText.translate(str.maketrans('', '', string.punctuation))


modified_text = modified_text_puncuation(myNewText)

def modified_text_capitalize_vowels(modified_text):
    char = set('aouei')
    for e in modified_text:
        if e in char:
            e = e.upper()
            print(e, sep=';', end='')
        else:
            print(e, sep=';', end='')  
    return e

numbers = modified_text_capitalize_vowels(modified_text)

def modification_numbers(numbers):
    for n in numbers:
        if n == 1:
            print('one')
        elif n == 2:
            print('two')
        elif n == 3:
            print('three')
        elif n == 4:
            print('four')
        elif n == 5:
            print('five')
        elif n == 6:
            print('six')
        elif n == 7:
            print('seven')
        elif n == 8:
            print('eight')
        elif n == 9:
            print('nine')
        elif n == 0:
            print('zero')


print(modification_numbers(numbers))

ничего не меняется, числа все также числа


Ответы (1 шт):

Автор решения: Salokhiddin Rakhmonkulov

Воспользуйтесь функцией str.replace(old, new[, count])

Заметьте, в функции modification_numbers() Вы сравниваете разные типы str и int.

Можете также завести словарь со значениями что на что менять и просто пройтись по нему циклом.

Пример:

text = '''SMS messaging was used for the first time on 3 December 1992,[8]
when Neil Papworth, a 22-year-old test engineer for Sema Group in the UK[9] (now Airwide 
Solutions),[10] used a personal computer to send the text message 'Merry Christmas'
via the Vodafone network to the phone of Richard Jarvis,[11][12] who was at a party
in Newbury, Berkshire, which had been organized to celebrate the event. Modern SMS
text messaging is usually messaging from one mobile phone to another. Finnish 
Radiolinja became the first network to offer a commercial person-to-person SMS text 
messaging service in 1994.'''

import string

d = {
    '1': 'one',
    '2': 'two',
    # ...
}

def change_text(text):
    mod_text = text
    
    for key, value in d.items():
        mod_text = mod_text.replace(key, value)
        
    return mod_text
        

print(change_text(text))

Результат:

SMS messaging was used for the first time on 3 December one99two,[8]
when Neil Papworth, a twotwo-year-old test engineer for Sema Group in the UK[9] (now Airwide 
Solutions),[one0] used a personal computer to send the text message 'Merry Christmas'
via the Vodafone network to the phone of Richard Jarvis,[oneone][onetwo] who was at a party
in Newbury, Berkshire, which had been organized to celebrate the event. Modern SMS
text messaging is usually messaging from one mobile phone to another. Finnish 
Radiolinja became the first network to offer a commercial person-to-person SMS text 
messaging service in one994.
→ Ссылка