Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I wanted to make a function of pangram

enter image description here

i coded this

def panagram(sen):
    sen= sen.replace(" ","")
    alphabets = ["a","b","c","d","e","f","g","h","i",'j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',1]
    for words in sen:
        for letter in words:
            if letter.lower() in alphabets:
                alphabets.remove(letter)
            else:
                pass
    if len(alphabets)==1:
        return "PANAGRAM"
    else:
        return "Not Panagram"

but when i run it i got an errorenter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

(PS i am a beginner this is my first question)

>Solution :

The problem is that you removed the spaces from the string.
When the first loop is run it, as there are no spaces in the string, it instead of considering words directly takes up the letters.
For eg. if

sen = "The quick brown fox jumps over the lazy dog"

when you remove spaces, it becomes

"Thequickbrownfoxjumpsoverthelazydog"

The word then becomes a letter that means in first run

word = T

And then there is nothing itterable in a string character, that’s why it gives error

You can either remove

sen= sen.replace(" ","")

Or the loop

for words in sen:

Just use

for letter in sen:

Also remove letter.lower() not lower, there is another problem in your string the letter h come twice, so when it is removed first time, there is nothing left to remove the second time, you can solve this by creating a set

a = set(sen)

Then you iterate through the set

Hope that helps!!

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading