0
why my program isn't running
def count_char(text,char): count=0 for c in text: if c ==char: count+=1 return count filename=input("Enter a file name: ") with open(filename) as f: text=f.read() for char in "abcdefghijklmnopqrstuvwxyz": prec=100*count_char(text,char)/ len(text) print("{0}-{1}%".format(char,round(perc,2)))
3 Answers
+ 2
Try this there is indentation error
def count_char(text,char):
count=0
for c in text:
if c ==char:
count+=1
return count
filename=input("Enter a file name: ")
with open(filename) as f:
text=f.read()
for char in "abcdefghijklmnopqrstuvwxyz":
prec=100*count_char(text,char)/len(text)
print("{0}-{1}%".format(char,round(perc,2)))
+ 2
I see that this is a code example from this lesson:
https://www.sololearn.com/learning/2457/
This is working, you just have to input a valid filename, or replace the input by a valid filename.
+ 1
In count_char() it looks like the return statement is indented too far.
In the for loop, the variable perc is misspelled as prec. Also, the print() statement should be indented so that it executes within the for loop.