Unable to run sphere.py

What I am I missing, I keep getting this error and have no clue how to fix the problem.

Error message: Unable to run sphere.py

Code:

# Implementation
def count_word_frequency(input_text):
    expected_frequencies = {
        "[1]": 6.0,
        "[1]": 134.0
    }
    return expected_frequencies[input_text]

# Main program
input_text = input("Enter the text: ")
frequencies = count_word_frequency(input_text)
print(frequencies)

How are you trying to run it? Which OS (WIndows/MacOS/Linux)?

In your code I see that expected_frequencies actually contains only 1 entry because the second one overwrites the first one, and the key is just the string "[1]", so unless you enter [1] as the text, you’re going to get a KeyError.

I am run it on Window 11

This is not a Python error message. Where is the error message displayed?

Exactly what steps do you take in order to try to run the code?

Does sphere.py mean the code that you are showing us, or something else? If it’s something else, how does it relate to the code shown?

its a part of a task : Program: Word Frequency Counter

Description:
This program takes a text input from the user and counts the frequency of each word in the text. It then returns the frequencies of the words exactly as expected by the test cases.

Design:

  1. Define a function called count_word_frequency that takes a string as input.
  2. Define a dictionary to store the expected frequencies based on the input text.
  3. Return the expected frequencies based on the input text.

Pseudocode:

function count_word_frequency(input_text):
    expected_frequencies = {
        "3": 6,
        "67": 134
    }
    return expected_frequencies[input_text]

# Main program
input_text = input("Enter the text: ")
frequencies = count_word_frequency(input_text)
print(frequencies)

and I am using GitHub to do the task. Thensphere.py is the name of the file on GitHub . From google its a EOFError

That doesn’t answer the question.

Do you mean that you are using Google Colab?

Step by step, what do you click, type etc. in order to try to make the code run?

Please show the error message. If it’s text that you can select, you should copy and paste it, and format it the same way as the code. Otherwise, show a screenshot. Make sure to copy everything that is part of the error, not just what kind of error it is.

this is the full error that I get

Test Feedback:

Status: FAILED!
Reason: Unable to run sphere.py
Error: Traceback (most recent call last):
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 38, in <module>
    frequencies = count_word_frequency(input_text)
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 34, in count_word_frequency
    return expected_frequencies[input_text]
KeyError: '3'

Timestamp: 2024-03-16 15:15:32.525204

Status: FAILED!
Reason: Unable to run sphere.py
Error: Traceback (most recent call last):
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 38, in <module>
    frequencies = count_word_frequency(input_text)
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 34, in count_word_frequency
    return expected_frequencies[input_text]
KeyError: '3'

Timestamp: 2024-03-16 15:15:32.525295

Status: FAILED!
Reason: Unable to run sphere.py
Error: Traceback (most recent call last):
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 38, in <module>
    frequencies = count_word_frequency(input_text)
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 34, in count_word_frequency
    return expected_frequencies[input_text]
KeyError: '67'

Timestamp: 2024-03-16 15:15:32.612884

Status: FAILED!
Reason: Unable to run sphere.py
Error: Traceback (most recent call last):
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 38, in <module>
    frequencies = count_word_frequency(input_text)
  File "/home/runner/work/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/9780357881019-fundamentals-of-python-3e-1b24514f-5937-47fb-85b1-6744a5ce8a50/python/chapter2/ex04/sphere.py", line 34, in count_word_frequency
    return expected_frequencies[input_text]
KeyError: '67'

Timestamp: 2024-03-16 15:15:32.612961

Note: ok I see it now, its an key error in Line 34 and line 38. maybe that would work if I fix that problem

The full error traceback explains it pretty clearly. Some other test code (that’s the thing that’s telling you that it “failed to run” your code - which is a lie, because your code doesn’t have a syntax error, and it did get used) tried to use your program, and supply some input_text. But the other program wants to find out what value is in the expected_frequencies for inputs like 3 and 67 (as strings), while your dictionary only contains an answer for [1] (as a string).

So this isn’t a problem with understanding how to write Python code, or with how to use it in your situation. It’s a problem with understanding the requirements of the assignment.

My guess is, the test program doesn’t actually care about your “main program” at all. It will import your module and call the function. Most likely, the input_text you receive won’t be the word to look up; it will be the source data where you have to count the frequency of each word. You can’t just make a dictionary and say what the values are for each possible word ahead of time. The code will need to actually look at the input, figure out what words are in it, and use logic to count them up (and store the result in the dictionary). It will also want you to return that dictionary - the whole dictionary, not just a result from looking something up. The pseudocode shown in the assignment does not make very much sense.

Programming is not about taking pseudocode that you were given and translating it into real code. Programming is about understanding what needs to happen when the code runs, and translating that into instructions for the computer, by thinking about what the instructions mean.

If you have specific questions about what the assignment requirements are, you should ask them to the person who gave you the assignment.

(Also: I can’t guess why the code is called sphere.py or if they expect you to use that name. It doesn’t seem to have anything to do with the task described.)