Python Forum

Full Version: Functions to remove vowels and extra blanks
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Example A:
Input:

Um cidadão ficou sem paciência ao esperar nas Finanças.

O âmbito do atentado foi grande. Ouve 56 vítimas. Que horror!

O coração foi o único órgão afetado, mas o doente não sobreviveu

.

Output:

m cdd fc sm pcnc sprr ns Fnnçs.

mbt d tntd f grnd. v 56 vtms. Q hrrr!

crç f nc rg ftd, ms dnt n sbrvv

Example B:
Input:

Ele já sabia que ia ser assim, tu é que achavas que não

.

Output:

l j sb q sr ssm, t q chvs q n

def isVowel(s):
if s in ('aeiouáéíóúàèìòùãõâêîôûAEIOUÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛ'):

    return True
else:
    return False
def eraseVowels(cadeia):
    vogais ='aeiouáéíóúàèìòùãõâêîôûAEIOUÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛ'
    nova_cadeia =''
    for car in cadeia:
        if car in vogais:
            nova_cadeia = nova_cadeia + ''
        else:
            nova_cadeia = nova_cadeia + car
    return nova_cadeia
def eraseExtraBlanks(s):
    while '  ' in s:
        s=s.replace('  ',' ')
        s.strip()

return s.strip()

x = input()
while x != '.':
    x = input()
    if x == '.':
        break
    if ('  ') in x:
        print(eraseExtraBlanks

print(eraseVowels)
I need the program to accept multiple strings until the user enters '.' then is should remove vowels and extra blanks(in case they exist) and show the results aas demonstrated in the examples
#!/usr/bin/env python3
import sys
def isVowel(s):
    if s in 'aeiouáéíóúàèìòùãõâêîôûAEIOUÁÉÍÓÚÀÈÌÒÙÃÕÂÊÎÔÛ':
        return True
    else:
        return False

def eraseVowels(cadeia):
    nova_cadeia =''
    for car in cadeia:
        if isVowel(car):
            nova_cadeia += ''
        else:
            nova_cadeia += car
    return nova_cadeia

def eraseExtraBlanks(s):
    while '  ' in s:
        s=s.replace('  ',' ')
        s.strip()
    return s.strip()

while True:
    x = input("input: ")
    if x == '.':
        break
    x = eraseExtraBlanks(x)
    x = eraseVowels(x)
    print(x)
Thank you it is working! But i need something that looks like this:
Example A:
Input:

Um cidadão ficou sem paciência ao esperar nas Finanças.

O âmbito do atentado foi grande. Ouve 56 vítimas. Que horror!

O coração foi o único órgão afetado, mas o doente não sobreviveu

.

Output:

m cdd fc sm pcnc sprr ns Fnnçs.

mbt d tntd f grnd. v 56 vtms. Q hrrr!

crç f nc rg ftd, ms dnt n sbrvv

Reads multiple strings and then gives me the result os them all in a row. Your solution only reads one string and gives me the output straight away.
I assume you can combine the two programs yourself.

#!/usr/bin/env python3
import sys

def multi_line_input():
    lines = ""
    while True:
        line = input("input: ")
        if line == '.':
            break
        else:
            lines += line + "\n"
    return lines


x = multi_line_input()
print(x)
That just gives me the string that i wrote or maybe i joined the programs wrong. Can you add that part to your previous answer? Please, that would really be helpfull this is kinda urgent
Replace
x = input(...)
 
with
x=multi_lines_input()
Did that! Thanks! Getting this error:
Traceback (most recent call last):
File "C:\Users\Utilizador\Desktop\5.2.py", line 58, in <module>
x = eraseVowels(x)
File "C:\Users\Utilizador\Desktop\5.2.py", line 27, in eraseVowels
for car in cadeia:
TypeError: 'NoneType' object is not iterable
#!/usr/bin/env python3
import sys

def isVowel(s):
    if s in 'aeiou':
        return True
    else:
        return False

def eraseVowels(cadeia):
    nova_cadeia =''
    for car in cadeia:
        if isVowel(car):
            nova_cadeia += ''
        else:
            nova_cadeia += car
    return nova_cadeia

def eraseExtraBlanks(s):
    while '  ' in s:
        s=s.replace('  ',' ')
    return s.strip()

def multi_line_input():
    lines = ""
    while True:
        line = input("input: ")
        if line == '.':
            break
        else:
            lines += line + "\n"
    return lines

x = multi_line_input()
x = eraseExtraBlanks(x)
x = eraseVowels(x)
print(x)
#done
Thanks a lot!
You can reduce the code.

def isVowel(s):
    if s in 'aeiou':
        return True
    else:
        return False
to

def isVowel(s):
    return s.lower() in 'aeiou'
Erasing vowels:

def eraseVowels(cadeia):
    return ''.join(char for char in cadeia if not isVowel(char))
Pages: 1 2