I am an assistant professor at an R1 university. I am advising an undergraduate student whose major is mechanical engineering. He tried really hard but cannot figure out how to do a python code (~300 lines) for an entire semester. He came up with some code but it does not work. His research progress has just stopped there for several weeks.

What can I do to help him, debug the code for him? I don't think it is common to debug for a student (especially an undergraduate). Are there any other strategies that would be more appropriate?

  • 2
    I think more details are needed: Is he trying and failing to understand existing code? Trying to adapt it? Trying to come up with new code? I cannot tell from the question.
    – Anonymous
    May 21, 2023 at 2:59
  • 2
    Let me preface this by saying I don't mean the following to be condescending, but am just trying to feel out the situation: Is yours and your student's goal for the project for him to pick up programming skills? Have you investigated with him whether he really has enough background for this project? Have you tried anything intermediate rather than just debugging code, e.g. suggesting a high-level interface (functions w/ signatures etc.) to help him break the problem down into small, testable units?
    – user137975
    May 21, 2023 at 3:40
  • Recommend reviewing the second half of my answer here.
    – cag51
    May 21, 2023 at 23:06
  • @AnonymousM You are right. I decided to advise him because his email made me feel like he knows a lot. However, it turns out he has no idea about programming. If I don't know about the concept, I will not say the words to others and pretend I know it. May 22, 2023 at 3:11

4 Answers 4


Realistically, the student isn't going to get this done on their own at the end of the semester. I would take the time to review the code with the student, discuss any obvious errors that I can see, and work with the student to develop a plan to get the code working in stages.

From a software engineering point of view, 300 lines in one function is unmanageable, especially for a student who is new to programming. You should start by reviewing the design at a high level, and then refactor it into smaller functions (and if the student has the appropriate background, objects) A test-driven programming approach can be especially helpful with students who have limited experience in programming.

  • 4
    I don't see any evidence in the (sketchy) question that it's a single function? May 21, 2023 at 5:39
  • Agreed- I should have included an "if" in that second paragraph. May 21, 2023 at 14:21

Code is not so different from any other assignment. What would you do if a student submitted a project report in which the final conclusions are wrong, presumably because of incorrect data, or incorrect interpretation of data? You could just write "wrong" on the assignment. But I bet you'd probably try to read through the report and figure out where the student went wrong -- primarily because you want the student to learn something, and just saying "wrong" is not conducive to this.

In other words, if your goal is to teach students skills that are useful to them, your job is to debug the code. I've often found it useful to do this side-by-side with the students so they can see how I approach doing this, what tools I use, etc.


Not "for" but with.

Ideally of course, and assuming you can find the time.

I've supervised experimental projects in which a similar amount of Python is an important part. The students have encountered Python before, but mainly analysing/plotting data in a fairly limited context, while for my project hardware interfacing and flow control were more important. I provided the simplest possible readout function to avoid them getting stuck on a very steep bit of learning curve. The Python part of the project was clear up front.

Some students needed no help. Many of these had additional prior programming experience, but by no means all.

Most needed a little guidance - I could give them pointers and they could debug.

A tiny minority could have been considered lost causes, but that was because of failure to engage with the support they were offered.

The ones of interest here had the capability to debug their code, but had never done so. So they needed help to get from "My code doesn't work", through defining working/not working, examining error messages and output, checking execution flow and intermediate variables, and on to correcting the code.

As far as I was concerned, debugging experiments* was a key learning outcome of the project; the code was part of that. And, here at least, the point of the project is that to go with more independence, they have more targetted support.

* For example, a couple of times they got strange results caused by sunlight reflecting off another building, through a small gap, and into the photodiode they were using. It was only then that they understood my insistence on logging against wall-clock time, not calculated elapsed time (which tended to drift as they started with the implicit assumption that time.sleep (30) in a loop meant the loop executed once every 30 seconds)

  • Regarding the footnote, perhaps your students got confused because wall-clock time (as well as wall time) typically also refers to an elapsed time, just measured in terms of actual time.
    – Anyon
    May 22, 2023 at 14:26
  • @Anyon no, they'd never come across the term, and I tended not actually use it in that context or with my students (only when trying to be brief e.g. here). However when I've seen wall-clock time used to mean elapsed (e.g. in contrast to CPU time which might parallelise) it's always been clear from the wording or context that it's a time difference and not a timestamp. But I'm familiar with it from a very long time ago, so my usage may be dated. "Timestamp" is a term I introduced them to.
    – Chris H
    May 22, 2023 at 14:36
  • ... Certainly the Jargon file supports the point-in-time definition: 1. ‘Real world’ time (what the clock on the wall shows), as opposed to the system clock's idea of time. This was on Raspberry Pi 2 systems, which lack a real-time clock when powered off, so the system clock's idea of time could be badly off without NTP (which I handled for them)
    – Chris H
    May 22, 2023 at 14:42
  • It's certainly one of the definitions of the term, but these days I think people are more likely to go off, e.g., Wikipedia, which only mentions the time-difference definition. "Timestamp" should be clear enough, IMO. But sometimes it doesn't matter how clear one is if the student looks up how to do something using an imprecise search statement and just runs with the first answer... Anyway, hopefully it made for a memorable lesson.
    – Anyon
    May 22, 2023 at 14:49
  • @Anyon I was surprised to find not even a mention in Wikipedia of the non-elapsed definition. Of course if they'd interpreted it as real (measured, not assumed) elapsed time we could have worked with that and the file creation time to reconstruct the bug occurrence time. Even if I told them to use a timestamp to ISO8601 some of them still would have added 30s to the previous timestamp. A simple loop would have been about 30.2s. It could rise to about 35s, or by using a timer, get pretty close to 30s, but always be long, never short, so there would always be drift
    – Chris H
    May 22, 2023 at 14:59

If your student cannot complete the assignment you gave him, then you gave him an assignment that does not match his level of knowledge and skills (an easy mistake to make, and one that I’ve probably made many times, sadly). The correct thing to do is to modify the assignment or replace it altogether with an assignment that the student can complete on his own. If you simply debug the assignment for him, that is a workable solution, but probably the student will end up learning very little from the project, other than getting the (very likely mistaken) impression that he is bad at programming and is not suited for technical work of any kind.

As for the code, if you need it as part of your larger research program, then finish it yourself or find another student to finish it who has the skills to do it.

  • I completely agree with you especially for the last sentence. It is a part of my larger research program. But I just cannot find qualified student who can do it. Sadly. I think I just wasted too much time in my first year. May 25, 2023 at 3:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .