Error in program


# Determine and display the proportion of words that include each letter of the alphabet. The

# letter that is used in the smallest proportion of words is highlighted at the end of the

# program’s output.

# creating a dictionary with letters of the alphabet

alphabet ={'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}

#opening file

f = open('text.txt','r')

#reading file

f_contents = f.read()

#remove the , and .

for line in f_contents:

    line.rstrip(",")

    line.rstrip(".")

    line.rstrip("!")

    line.lstrip(",")

    line.lstrip(".")

    line.lstrip("!")

#remove all upper case

for line in f_contents:

    line.lower()

# update the key count

for line in f_contents:

    for letter in line:

        if letter == 'a':

            alphabet ['a'] = alphabet ['a'] + 1

        elif letter == 'b':

            alphabet ['b'] = alphabet ['b'] + 1

        elif letter == 'c':

            alphabet ['c'] = alphabet ['c'] + 1

        elif letter == 'd':

            alphabet ['d'] = alphabet ['d'] + 1

        elif letter == 'e':

            alphabet ['e'] = alphabet ['e'] + 1

        elif letter == 'f':

            alphabet ['f'] = alphabet ['f'] + 1

        elif letter == 'g':

            alphabet ['g'] = alphabet ['g'] + 1

        elif letter == 'h':

            alphabet ['h'] = alphabet ['h'] + 1

        elif letter == 'i':

            alphabet ['i'] = alphabet ['i'] + 1

        elif letter == 'j':

            alphabet ['j'] = alphabet ['j'] + 1

        elif letter == 'k':

            alphabet ['k'] = alphabet ['k'] + 1

        elif letter == 'l':

            alphabet ['l'] = alphabet ['l'] + 1

        elif letter == 'm':

            alphabet ['m'] = alphabet ['m'] + 1

        elif letter == 'n':

            alphabet ['n'] = alphabet ['n'] + 1

        elif letter == 'o':

            alphabet ['o'] = alphabet ['o'] + 1

        elif letter == 'p':

            alphabet ['p'] = alphabet ['p'] + 1

        elif letter == 'q':

            alphabet ['q'] = alphabet ['q'] + 1

        elif letter == 'r':

            alphabet ['r'] = alphabet ['r'] + 1

        elif letter == 's':

            alphabet ['s'] = alphabet ['s'] + 1

        elif letter == 't':

            alphabet ['t'] = alphabet ['t'] + 1

        elif letter == 'u':

            alphabet ['u'] = alphabet ['u'] + 1

        elif letter == 'v':

            alphabet ['v'] = alphabet ['v'] + 1

        elif letter == 'w':

            alphabet ['w'] = alphabet ['w'] + 1

        elif letter == 'x':

            alphabet ['x'] = alphabet ['x'] + 1

        elif letter == 'y':

            alphabet ['y'] = alphabet ['y'] + 1

        elif letter == 'z':

            alphabet ['z'] = alphabet ['z'] + 1

# print the results 

for key,value in alphabet.items():

    print("key value pairs",key,value)

# find min value and its key

min_value = min(alphabet.values())

print("the minimum value is ",min(value))

#minimum value's key

letter_min = alphabet.index(min_value)

print("the value's minimum key is", letter_min)

I am getting this error

PS C:\Users\simmy.vscode> & C:/Users/simmy/AppData/Local/Programs/Python/Python39-32/python.exe c:/Users/simmy/.vscode/test.py
key value pairs a 6
key value pairs b 2
key value pairs c 1
key value pairs d 0
key value pairs e 5
key value pairs f 1
key value pairs g 2
key value pairs h 3
key value pairs i 3
key value pairs j 1
key value pairs k 1
key value pairs l 3
key value pairs m 2
key value pairs n 3
key value pairs o 6
key value pairs p 0
key value pairs q 0
key value pairs r 4
key value pairs s 6
key value pairs t 3
key value pairs u 3
key value pairs v 0
key value pairs w 0
key value pairs x 0
key value pairs y 2
key value pairs z 0
Traceback (most recent call last):
File “c:\Users\simmy.vscode\test.py”, line 96, in
print("the minimum value is ",min(value))
TypeError: ‘int’ object is not iterable
PS C:\Users\simmy.vscode

The problem appears to be arising here. the min() function requires an object passed in that is an iterable i.e. a dictionary, list etc. However value is just a single number and cannot be iterated over, hence the error message. I believe what you wanted was instead of min(value), you wanted the variable min_value declared on the previous line.

1 Like

oh ya… that is exactly what l wanted, thanks

Traceback (most recent call last):
File “c:\Users\simmy.vscode\test.py”, line 98, in
print("the minimum value is ",min(min_value))
TypeError: ‘int’ object is not iterable

sorry l couldn’t test the program, l thought that should solve it

I am still getting the error, why is min not working on it, the solution had the same line of code

Thanks

You don’t need to use min() as there’s only one value assigned to that variable. min() is used to find the smallest value of a group, so for example min([2, 3, 4]) would return 2. however, min_value is just a single value, and you cannot find the “minimum” of one value, it needs to be a list. Therefore you should just do:

print("the minimum value is ", min_value)

# Determine and display the proportion of words that include each letter of the alphabet. The

# letter that is used in the smallest proportion of words is highlighted at the end of the

# program’s output.

# creating a dictionary with letters of the alphabet

alphabet ={'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}

#opening file

f = open('text.txt','r')

#reading file

f_contents = f.read()

#remove the , and .

for line in f_contents:

    line.rstrip(",")

    line.rstrip(".")

    line.rstrip("!")

    line.lstrip(",")

    line.lstrip(".")

    line.lstrip("!")

#remove all upper case

for line in f_contents:

    line.lower()

count = 0

# count number of lines/words in the file

for line in f_contents:

    if line != "\n":

        count += 1

print("the number of lines are", count)

# update the key count

for line in f_contents:

    for letter in line:

        if letter == 'a':

            alphabet ['a'] = alphabet ['a'] + 1

        elif letter == 'b':

            alphabet ['b'] = alphabet ['b'] + 1

        elif letter == 'c':

            alphabet ['c'] = alphabet ['c'] + 1

        elif letter == 'd':

            alphabet ['d'] = alphabet ['d'] + 1

        elif letter == 'e':

            alphabet ['e'] = alphabet ['e'] + 1

        elif letter == 'f':

            alphabet ['f'] = alphabet ['f'] + 1

        elif letter == 'g':

            alphabet ['g'] = alphabet ['g'] + 1

        elif letter == 'h':

            alphabet ['h'] = alphabet ['h'] + 1

        elif letter == 'i':

            alphabet ['i'] = alphabet ['i'] + 1

        elif letter == 'j':

            alphabet ['j'] = alphabet ['j'] + 1

        elif letter == 'k':

            alphabet ['k'] = alphabet ['k'] + 1

        elif letter == 'l':

            alphabet ['l'] = alphabet ['l'] + 1

        elif letter == 'm':

            alphabet ['m'] = alphabet ['m'] + 1

        elif letter == 'n':

            alphabet ['n'] = alphabet ['n'] + 1

        elif letter == 'o':

            alphabet ['o'] = alphabet ['o'] + 1

        elif letter == 'p':

            alphabet ['p'] = alphabet ['p'] + 1

        elif letter == 'q':

            alphabet ['q'] = alphabet ['q'] + 1

        elif letter == 'r':

            alphabet ['r'] = alphabet ['r'] + 1

        elif letter == 's':

            alphabet ['s'] = alphabet ['s'] + 1

        elif letter == 't':

            alphabet ['t'] = alphabet ['t'] + 1

        elif letter == 'u':

            alphabet ['u'] = alphabet ['u'] + 1

        elif letter == 'v':

            alphabet ['v'] = alphabet ['v'] + 1

        elif letter == 'w':

            alphabet ['w'] = alphabet ['w'] + 1

        elif letter == 'x':

            alphabet ['x'] = alphabet ['x'] + 1

        elif letter == 'y':

            alphabet ['y'] = alphabet ['y'] + 1

        elif letter == 'z':

            alphabet ['z'] = alphabet ['z'] + 1

# print the results 

for key,value in alphabet.items():

    print("key value pairs",key,value)

# remove the zeros from the dictionary

for key in alphabet.keys():

    if alphabet[key] == 0:

        del alphabet[key]

# find min value and its key

min_value = min(alphabet.values())

print("the minimum value is ",min_value)

#minimum value's key

def get_key(min_value):

    for key, value in alphabet.items():

         if min_value == value:

             return key

 

    return "key doesn't exist"

print("the value's minimum key is", key)

I am having a problem in removing keys with zero values and counting number of lines

Thanks

Have you tried inserting a few print statements to observe the objects you’re trying to modify?

for counting no of lines am not sure if print statements can help

this is the error

the number of lines are 57
key value pairs a 6
key value pairs b 2
key value pairs c 1
key value pairs d 0
key value pairs e 5
key value pairs f 1
key value pairs g 2
key value pairs h 3
key value pairs i 3
key value pairs j 1
key value pairs k 1
key value pairs l 3
key value pairs m 2
key value pairs n 3
key value pairs o 6
key value pairs p 0
key value pairs q 0
key value pairs r 4
key value pairs s 6
key value pairs t 3
key value pairs u 3
key value pairs v 0
key value pairs w 0
key value pairs x 0
key value pairs y 2
key value pairs z 0
Traceback (most recent call last):
File “c:\Users\simmy.vscode\test.py”, line 109, in
for key in alphabet.keys():
RuntimeError: dictionary changed size during iteration

The no of lines in text.txt is only 10 and its giving 57

It sounds like you’re mutating your dictionary as you iterate through it. Try thinking of a solution where you don’t change the thing your iterating on (this would apply with lists and strings as well).


# Determine and display the proportion of words that include each letter of the alphabet. The

# letter that is used in the smallest proportion of words is highlighted at the end of the

# program’s output.

# creating a dictionary with letters of the alphabet

alphabet ={'a':0,'b':0,'c':0,'d':0,'e':0,'f':0,'g':0,'h':0,'i':0,'j':0,'k':0,'l':0,'m':0,'n':0,'o':0,'p':0,'q':0,'r':0,'s':0,'t':0,'u':0,'v':0,'w':0,'x':0,'y':0,'z':0}

#opening file

f = open('text.txt','r')

#reading file

f_contents = f.read()

#remove the , and .

for line in f_contents:

    line.rstrip(",")

    line.rstrip(".")

    line.rstrip("!")

    line.lstrip(",")

    line.lstrip(".")

    line.lstrip("!")

#remove all upper case

for line in f_contents:

    f_contents.lower()

count = 0

# count number of lines/words in the file

colist = f_contents.split("\n")

for item in colist:

    if item:

        count += 1

print("the number of lines are", count)

# update the key count

for line in f_contents:

    for letter in line:

        if letter == 'a':

            alphabet ['a'] = alphabet ['a'] + 1

        elif letter == 'b':

            alphabet ['b'] = alphabet ['b'] + 1

        elif letter == 'c':

            alphabet ['c'] = alphabet ['c'] + 1

        elif letter == 'd':

            alphabet ['d'] = alphabet ['d'] + 1

        elif letter == 'e':

            alphabet ['e'] = alphabet ['e'] + 1

        elif letter == 'f':

            alphabet ['f'] = alphabet ['f'] + 1

        elif letter == 'g':

            alphabet ['g'] = alphabet ['g'] + 1

        elif letter == 'h':

            alphabet ['h'] = alphabet ['h'] + 1

        elif letter == 'i':

            alphabet ['i'] = alphabet ['i'] + 1

        elif letter == 'j':

            alphabet ['j'] = alphabet ['j'] + 1

        elif letter == 'k':

            alphabet ['k'] = alphabet ['k'] + 1

        elif letter == 'l':

            alphabet ['l'] = alphabet ['l'] + 1

        elif letter == 'm':

            alphabet ['m'] = alphabet ['m'] + 1

        elif letter == 'n':

            alphabet ['n'] = alphabet ['n'] + 1

        elif letter == 'o':

            alphabet ['o'] = alphabet ['o'] + 1

        elif letter == 'p':

            alphabet ['p'] = alphabet ['p'] + 1

        elif letter == 'q':

            alphabet ['q'] = alphabet ['q'] + 1

        elif letter == 'r':

            alphabet ['r'] = alphabet ['r'] + 1

        elif letter == 's':

            alphabet ['s'] = alphabet ['s'] + 1

        elif letter == 't':

            alphabet ['t'] = alphabet ['t'] + 1

        elif letter == 'u':

            alphabet ['u'] = alphabet ['u'] + 1

        elif letter == 'v':

            alphabet ['v'] = alphabet ['v'] + 1

        elif letter == 'w':

            alphabet ['w'] = alphabet ['w'] + 1

        elif letter == 'x':

            alphabet ['x'] = alphabet ['x'] + 1

        elif letter == 'y':

            alphabet ['y'] = alphabet ['y'] + 1

        elif letter == 'z':

            alphabet ['z'] = alphabet ['z'] + 1

# print the results 

for key,value in alphabet.items():

    print("key value pairs",key,value)

# remove the zeros from the dictionary

for key,value in list(alphabet.items()):

    if value == 0:

        del alphabet[key]

print("the dictionary is ", alphabet) 

# find min value and its key

min_value = min(alphabet.values())

print("the minimum value is ",min_value)

#minimum value's key

for keys in alphabet.keys():

    if alphabet[keys] == min_value:

        print(keys)

# calculating the proportion of the minimum words

proportion = (min_value / count) * 100

print("the proportion is", proportion,"%")

I managed how to code it

Thanks toasted pitabread