Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

A Method That Doesn't Exist

An assistant writes its answer word by word, each time choosing what fits best. Usually, what fits is what it learned. But a name can fit and still not exist: a method that sounds like one Python ought to have, or a library's function under a name it never had. This is called a hallucination, and the made-up name is written just as surely as the real ones around it. Reading the code won't tell you which is which. Running it will: Python stops at a name that doesn't exist.

The program in the editor is the kind of answer someone might get when they ask for a function that works out Mia's average walk. Asked plainly for a program this short, an assistant rarely makes up a method of a list; it happens more with libraries it has seen less of. This one is here to show you what it looks like when it does. Run it, and it ends with this error:

Traceback (most recent call last):
  File "<exec>", line 5, in <module>
  File "<exec>", line 2, in average_walk
AttributeError: 'list' object has no attribute 'average'

Read it from the bottom. An attribute is a name written after a dot, such as a method. A list has methods, append(), pop() and remove() among them, but none called average. The line above says where Python stopped: line 2, in average_walk.

There's nothing to install and nothing to repair in that line: ask for code without the made-up name. Send the error with the program, because it isn't a guess, it's what Python did. And ask for a version with only what you know: then every name in the answer is one you can check yourself.

Instructions

  1. 1

    Run the program and read the error in the console. Then attach script.py, and click Attach to message on the console as well, so that output.txt goes with it. Ask whether a list really has a method called average(), and for a version of average_walk() with only what you know, such as sum() and len(). Then send it.

  2. 2

    Copy the new average_walk() from the answer and paste it in place of the old one, keeping the lines under it. Run the program: it prints Average: 38.75.

Back

1/6

  1. 1. A Method That Doesn't Exist
  2. 2. Things That Aren't Here
  3. 3. Quiz: An Older Version
  4. 4. Try It in Three Lines
  5. 5. Quiz: Check the Claim
  6. 6. Summary
Next

Start programming for free

By signing up, you agree to the Terms of Service and Privacy Policy.

Or sign up with: