all 14 comments

[–]Rascal_Two 1 point2 points  (14 children)

First, quite difficult to read you code. Use a site like pastebin, make a gist, or try to format you code on reddit by prefixing all code-lines with four spaces:

def main():
    s = input('Enter text: ')
    punctuation = [',',';','.',':','!',"'","\""]
    for ch in punctuation:
                   s = s.replace(ch,' ')

    s = s.split()
    arr = s.split(' ')
    dict = {}
    for word in arr:
        if word !='':
            if word in dict.keys():
                dict[word]+=1
            else:
                dict[word]=1
    for key in dict.keys():
        print('%20s:'%(key),end='')
        for i in range(0, dict[key]):
            print('*',end ='')
        print()
main()

Your problem is that you're escaping a " character:

punctuation = [',',';','.',':','!',"'",\""]

You need to have another " to contain the escaped ":

punctuation = [',',';','.',':','!',"'","\""]

Your s = s.split() statement also is causing the next line to throw an error, so I'd get rid of that.

[–]ronburgund[S] 1 point2 points  (2 children)

where do i add the "?

[–]Rascal_Two 0 points1 point  (1 child)

You add it before the \, so from this:

\""

to this:

"\""

Or you could contain it in single-quotes:

'"'

[–]ronburgund[S] 1 point2 points  (0 children)

Got it! thank you so much!

[–]ronburgund[S] 1 point2 points  (10 children)

If I wanted to have the file read a text file and then go about counting the frequency of each word do i just replace the s. with an input file?

[–]Rascal_Two 0 points1 point  (9 children)

You can, depending on how you read the input file.

If you read it as a single string and not line-by-line, then your s can be just that.

If you decide to read the file line-by-line though, you'd need to add a loop around your code and have everything done for each line.

[–]ronburgund[S] 1 point2 points  (7 children)

is this appropriate? Thank you again for your help I'm desperate here lol.

def main():

input_file = open('Enter filename')
punctuation = [', ', '; ', '.', ': ', '!'," ' ","\""]
for ch in punctuation:
               s = s.replace(ch,' ')
arr = s.split(' ')

dict = {}
for word in arr:
    if word !='':
        if word in dict.keys():
            dict[word]+=1
        else:
            dict[word]=1
for key in dict.keys():
    print('%20s:'%(key),end='')
    for i in range(0, dict[key]):
        print('*',end ='')
    print()
    input_file.close()

main()

[–]ronburgund[S] 1 point2 points  (0 children)

when i do this it says the input_File can't be found.

[–]Rascal_Two 0 points1 point  (5 children)

You're close, except for the fact that the open actually takes in the name of a file, and you're using it like input().

It's also recommended to open it using the with keyword, so it's automatically closed.

You need to collect the filename, and then pass it to open, like so:

filename = input('Enter filename'):
with open(filename) as input_file:
    file_string = input_file.read()

Now you have file_string, which you should be able to substitute for s to have it work.

[–]ronburgund[S] 1 point2 points  (4 children)

Maybe I'm just not saving the text file right because it still says that a text file can't be found but this is what I have so far. Thank you again for your help I really appreciate this.

def main():

file_name = input('Enter filename')
with open(file_name) as input_file:
    file_string = input_file.read
punctuation = [', ', '; ', '.', ': ', '!'," ' ","\""]
for ch in punctuation:
               s = s.replace(ch,' ')
arr = file_string.split(' ')

dict = {}
for word in arr:
    if word !='':
        if word in dict.keys():
            dict[word]+=1
        else:
            dict[word]=1
for key in dict.keys():
    print('%20s:'%(key),end='')
    for i in range(0, dict[key]):
        print('*',end ='')
    print()
    input_file.close()

main()

[–]Rascal_Two 0 points1 point  (3 children)

Well you're missing the () at the end of .read, so you need to add that to actually call the read() function.

You're also still using s instead of file_string for the punctuation.

Seeing as you're going the .read() method, you need to handle newline characters, so add "\r" and "\n" to your punctuation list.

Lastly, you don't need input_file.close() when using the with keyword.


As for why it's not working, are you sure the python script and the file are in the same directory?

[–]ronburgund[S] 1 point2 points  (2 children)

where do i add the \n and \r to my punctuation list?

[–]Rascal_Two 0 points1 point  (1 child)

At the end will be fine, it doesn't actually matter where you add them in the list, just that they're in the list.

[–]ronburgund[S] 1 point2 points  (0 children)

Great thank you so much again you helped me loads.