why my program isn't running | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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)))

28th Jan 2021, 5:39 AM
Malik Shoaib
Malik Shoaib - avatar
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)))
28th Jan 2021, 5:52 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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.
28th Jan 2021, 5:53 AM
noteve
noteve - avatar
+ 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.
28th Jan 2021, 6:14 AM
Brian
Brian - avatar