samedi 24 novembre 2018

For loop and if conditions

I'm following an online course and I was working on this lesson when I tried to improvise and write something of my own. So, what the script should do it's simple: given a x word it should count the number of vowels and consonants:

vocali = 0
consonati = 0
parola = input("Inserisci parola: ")
singolare_voc = ""
singolare_cons = ""

for lettera in parola:
    if lettera in "aeiou":
        vocali = vocali + 1
    else:
        consonati = consonati + 1

        if vocali == 1:
            singolare_voc = "vocale"
        elif consonati == 1:
            singolare_cons = "consonante"
        else:
            singolare_voc = "vocali"
            singolare_cons = "consonanti"

string = "Nella parola {} ci sono {} {} e {} {}"
output = string.format(parola, vocali, singolare_voc, consonati, singolare_cons)

Now my problem is in this part:

if vocali == 1:
                singolare_voc = "vocale"
            elif consonati == 1:
                singolare_cons = "consonante"
            else:
                singolare_voc = "vocali"
                singolare_cons = "consonanti"

The propouse of this "if statement" is to change the italian word for vowel and consonant to singular or plurar according to the number of vowel and consonat counted. For instance if in a word there are 3 vowel I want to display: vocali (plurar form) and not vocale(singular form). I don't understan why but it works only with some words, for example if I insert something like: "aa", it doesn't display the variable as it should do or, at least, as I intended.

What I'm doing wrong?

Aucun commentaire:

Enregistrer un commentaire