Question

# I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic). I was...

I am working on exercise 5.30 from Introduction to Computing using python (Author: Perkovic).

I was looking at the solution and was able to understand what to do. However, when I implement the temp function as indicated, I keep getting this error "ValueError: the first two maketrans arguments must have equal length". However, it seems my two arguments are equal length, so I'm not sure what I am doing wrong!

print('Exercise 5.30')
def many(file):
infile = open(file)
infile.close

temp = str.maketrans('.,;!?:\n',7*'')

words = content.translate(temp).split()

count = [0,0,0,0]

for i in words:
if len(i)<= 4:
count[len(i)-1]+=1
for i in range(4):
print ('Words of length', len(words[i]),':',count[i])

print(many('sample.txt'))

Additionally, if I change the code to remove the translating function, why does it still include words with a char count > 4? I thought this statement removes it: if len(i)<= 4:

print('Exercise 5.30')
def many(file):
infile = open(file)
infile.close

words = content.split()

count = [0,0,0,0]

for i in words:
if len(i)<= 4:
count[len(i)-1]+=1
for i in range(4):
print ('Words of length', len(words[i]),':',count[i])

print(many('sample.txt'))

Lastly, the sample text includes: "This is a sample text file. This file containsa sample text but no sample or text. Text is in this file."

Thanks,

Karen

``````def many(file):
infile = open(file)
infile.close
#code modifies  second arg should be 7*' '
temp = str.maketrans('.,;!?:\n',7*' ')
words = content.translate(temp).split()

count = [0,0,0,0]

for i in words:
if len(i)<= 4:
count[len(i)-1]+=1
for i in range(4):
print ('Words of length', len(words[i]),':',count[i])
``````

In temp = str.maketrans('.,;!?:\n',7*' ') seocnd argument length should be same .

intitiall len(' 7*' ' ') would result in zero becuase len('') is zero .

after making it as 7*' ' which would give length as 7

#### Earn Coins

Coins can be redeemed for fabulous gifts.