0

My python program works fine however it keeps printing the answer atleast 5 times over and I am racking my brain as to why. Any ideas?

Text = """The University of Wisconsin–Milwaukee is a public urban research university in Milwaukee, Wisconsin. It is the largest university in the Milwaukee metropolitan area and a member of the University of Wisconsin System. It is also one of the two doctoral degree-granting public universities and the second largest university in Wisconsin. The university consists of 14 schools and colleges, including the only graduate school of freshwater science in the U.S., the first CEPH accredited dedicated school of public health in Wisconsin, and the state"s only school of architecture. As of the 2015–2016 school year, the University of Wisconsin–Milwaukee had an enrollment of 27,156, with 1,604 faculty members, offering 191 degree programs, including 94 bachelor's, 64 master's and 33 doctorate degrees. The university is classified among "R1: Doctoral Universities – Highest research activity". In 2018, the university had a research expenditure of $55 million. The university's athletic teams are the Panthers. A total of 15 Panther athletic teams compete in NCAA Division I. Panthers have won the James J. McCafferty Trophy as the Horizon League's all-sports champions seven times since 2000. They have earned 133 Horizon League titles and made 40 NCAA tournament appearances as of 2016."""

for punc in "–.,\n":
    Text=Text.replace(punc," ")
Text = Text.lower()
word_list = Text.split()

dict = {}

for word in word_list:
    dict[word] = dict.get(word, 0) + 1
    
    word_freq = []
    for key, value in sorted(dict.items()):
        if value > 5:
            print(key, value)
2
  • You nested your loops. Did you mean to put the inner loop outside the other loop? Commented Mar 16, 2022 at 16:56
  • FYI don't name something a built-in name like dict; you won't be able to access that anymore, and any scripts that you import which use dict() will fail. Commented Mar 16, 2022 at 16:58

1 Answer 1

1

You have an indentation issue that leads to the nested for loop. Fix the code into:

Text = """The University of Wisconsin–Milwaukee is a public urban research university in Milwaukee, Wisconsin. It is the largest university in the Milwaukee metropolitan area and a member of the University of Wisconsin System. It is also one of the two doctoral degree-granting public universities and the second largest university in Wisconsin. The university consists of 14 schools and colleges, including the only graduate school of freshwater science in the U.S., the first CEPH accredited dedicated school of public health in Wisconsin, and the state"s only school of architecture. As of the 2015–2016 school year, the University of Wisconsin–Milwaukee had an enrollment of 27,156, with 1,604 faculty members, offering 191 degree programs, including 94 bachelor's, 64 master's and 33 doctorate degrees. The university is classified among "R1: Doctoral Universities – Highest research activity". In 2018, the university had a research expenditure of $55 million. The university's athletic teams are the Panthers. A total of 15 Panther athletic teams compete in NCAA Division I. Panthers have won the James J. McCafferty Trophy as the Horizon League's all-sports champions seven times since 2000. They have earned 133 Horizon League titles and made 40 NCAA tournament appearances as of 2016."""

for punc in "–.,\n":
    Text=Text.replace(punc," ")
    
Text = Text.lower()
word_list = Text.split()

freq = {}

for word in word_list:
    freq[word] = freq.get(word, 0) + 1
    
for key, value in sorted(freq.items()):
    if value > 5:
        print(key, value)

Since the loops are nested, the print(key, value) line gets called whenever the outer loop goes to the next word. As your freq dictionary grows larger, it will inevitably keeps printing out that same dictionary for every iteration, leading to redundant printing.

=> You probably don't want that; you only want to print the freq dictionary only ONCE after the previous for loop has finished collecting the frequency of each word. Thus separating the loops - the second loop will only run after the first one finished.

Edit: Another thing pointed out by @random-davis is that you don't want to use reserved keyword like dict for your variable name. Change it to freq, or dictionary, or something else.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.