Arrrgh! Why Doesn’t My Python Program Work?

I’m having trouble with a tutorial… it doesn’t work!

I can’t figure out why my code isn’t working!

You’ve been banding your head against your desk for hours. The examples seemed so clear, but when you wrote your program it all fell apart. You’ve looked over the code, again and again. Everything looksfine. IT. JUST. DOESN’T. WORK.

You want your program to work correctly. You want to move past this annoying bug. You want to move on and keep learning. What can you do?

You can do a few things when you are stuck. You can write tests while while you write the program. You can talk to someone (or yourself!) else and get their help.

The Solution!

Use a debugger. A debugger allows you to interactively run a program and check the process state. Use a debugger to quickly explore how a program actually works. A debugger helps you understand Python better than passively reading example programs.

A Detailed Walkthrough

In this case, we will use the built-in Python debugger. The Python program we’re running is from a Reddit post, and the code is here.

First, in your console, start your program (we’re using Python 3.6 at the moment):

python -m pdb guesser.py

This starts the Python interpreter and loads the pdb (the Python debugger) module.

You’ll see the pdb prompt:

-> def main():
(Pdb)

This shows you that the program execution starts at the main function definition.

The debugger accepts a number of different commands. The first one we’ll use is the w command – which stands for where, and it will show you the current “line” in the program the debugger is about to execute. It also shows you the current “stack”, or the history of functions the interpreter executed in the program.

(Pdb) w
c:\program files\python36\lib\bdb.py(431)run()
-> exec(cmd, globals, locals)
(1)()
> c:\users\jjeffers\pydebug\guesser.py(1)()
-> def main():
(Pdb)

The bottom most function is the current executing “frame” of the program.

To get to the next line of the program (and have Python execute the current line), press n (or next).

> if __name__ == "__main__":
(Pdb)

Notice how the function definition was “skipped”, and the debugger jumped to the next executable line.

Enter n again.

-> main()
(Pdb)

Now we’re entered the if statement, because the test for __name__ == "__main__" was true. The debugger is showing you that the next line to execute is the call to the “main” function.

Enter s (or step) here. The step command works like next, but instructs the debugger to “step into” any function calls.

-> def main():
(Pdb)

Don’t worry, we’re not caught in a loop here. Enter n to move to the next line inside the main function.

-> print("Guess a number between 1 and 100.")
(Pdb)

Keep using n until you reach the line:

-< while not found:
(Pdb)

You will see the lines for the variable definitions for found and randomNumber. You can use the debugger to query the interpreter for the current value of a variable.

(Pdb) found
False

Let’s move on until you get to the prompt:

-> userGuess = input("Your Guess: ")
(Pdb)

Enter n again, then and enter 3 when prompted.

The very next line is

-> if userGuess == randomNumber:
(Pdb)

Here’s where the debugger really gets useful. Go ahead and query the test expression:

(Pdb) userGuess == randomNumber
False

This shows you where the issue is, but it might still not be clear why that test fails until we look at the operands userGuess and randomNumber.

(Pdb) userGuess
'3'
(Pdb) randomNumber
3

And that’s it – the 2 variables hold different values – one is a string and the other is an integer.

Using the debugger is a good skill to learn. It helps you track down issues in a running program. If you want to add more safety, you need to write tests, too.

If you want a detailed example of writing a test, check out Keep Calm and Add Unit Tests with Python.

Yes, You Should Add Unit Tests To Your Project

Should I go back on what I have in my project and write some tests for it, or just finish the project?

You’ve worked long and hard on your projects only to realize that you didn’t write any tests. You’ve read how important tests are. You probably also know this is a best practice, but it’s so much more work. Even worse, what if the tests show you that there are problems. Then you’ll have to work even more to fix them!

The truth is your project isn’t done. Every software product has bugs – you just haven’t found them yet. Of course, you want to do a good job. You want to show people you know the basics. You want people to know you are ready for the next step. But you know, deep down, the whole house of cards will collapse.

You’ll be exposed as a fraud who didn’t bother to test your work. No employer will touch you. You’ll end up living in cardboard box, down by the river, subsisting on a diet of government cheese.

You want tests. You want a lot of tests that give you confidence that when you make a change you’re not breaking things. You want to run your tests whenever you like. You want continuous integration to run those tests whenever you commit a change. You want the world to see you understand how to make safe changes.

The fix: write ONE test first.

You don’t have to blanket the project with tests all at once. In fact, if you try that, you’ll fail. You’ll lose motivation when you realize just how many tests you should create.

You need to create a momentum of success first. You need to have a small, solid win. From there you’ll pick up steam and get more tests in place.

  1. Find your unit test framework and install it.
  2. Find the part of your project that is the easiest to test.
    • Look for code where you have little or no dependencies. Hint: If it touches a network or file system, keep looking.
    • Find functionality where you can isolate a function or class method. Hint: If it takes setting up helper objects then keep digging into the helper objects.
  3. Create a test file anywhere. Don’t worry about the “right” place to put it.
  4. Run the test. Yes, it will fail.
  5. Write one test in a way that you know it will fail. For example, if you are testing a method that should return a number, have the test expect a string.
  6. Fix the test so it passes. Hint: this might take more than one try. That’s fine!
  7. When the test passes, commit that test.
  8. Take a break. Enjoy this feeling!

That’s it. That’s how you get started. Next step is to keep adding tests, one at a time.

If you want to see a detailed example of adding tests, check out

Get the next post right to your inbox!

Did you find this useful? Sign up and get more tips and help!
Email Address

How to Switch from “Exercise Coder” to Software Developer

I can do all the exercises, and I understand all the code, but I don’t know how to make a real project.

I cannot start any project, I cannot “build” anything. I feel so helpless. Maybe I’m not supposed to be a developer?

It’s not uncommon for new programmers to reach a point where they master the basics and are left wondering “What next?”

They feel despair or confusion. They look around and realize that taking another course or reading another blog article is going to make the answer appear.

The frustrated “exercise coder” wants to make the next great leap in their skills. They want to learn how to make real contributions and get something working.

An Easy Solution!

Fortunately there’s an excellent source of project work waiting for attention: open source software projects.

You can contribute to any number of software projects as a novice developer. How to Contribute to Open Source is an excellent source for finding and contributing to open source projects.

You’ll gain experience and learn from others while helping out with open source projects.

And there’s a bonus! You’ll develop a growing portfolio of work you can demonstrate to potential employers.

Here’s what you should do, right now:

    1. Follow @yourfirstpr on Twitter.
    2. Watch the tweets, and pick one of the issues that stands out to you.
    3. Make sure you read the How To Contribute guide.
    4. Approach the project organizers and ask if they’d like some help. Optionally, you can combine this a set of changes as a PR (pull request).

All of this assumes you are familiar with Git (and to some extent Github). If you aren’t yet familiar with Git, Github hosts this short introduction guide that will get you started.

A Very Useful Dictionary of Computing

When you learn a new skill it’s import to have a clear understanding of the the vocabulary.

A recent learnprogramming sub-reddit post asked about which terms are important. (That question itself is superb for learning.)

One response mentioned this dictionary of computing terms – the Free On-Line Dictionary of Computing Terms. This is a very handy resource to have at your fingertips.

First Steps: Write Your First Python Program in 30 Seconds

If you want to become a test automation engineers but are still thinking about learning how to program you need to get moving.

You need to take the first step. You need to push a little rock that will start an avalanche of momentum.

You are going to write your first program right now.

Go to Python.org and follow along:

This is an embedded Python interpreter. It’s probably not a tool you would use for anything beyond simple experimentation. You have to admit, it’s a very quick way to try your hand at some simple Python programs.

Introduction to Programming Course Comparison

There are many courses available to help you learn how to program.

Here is a list of several available online, in no particular order. None of these courses assume you have previous programming experience.

DISCLAIMER: I receive no compensation for these summaries.

Automate the Boring Stuff (Udemy)

Cost: $10 for the Udemy video course if you go through the link at https://automatetheboringstuff.com/, otherwise $50.

Time to Complete: 9.5 hours

Summary: Comprehensive review of Python through video lectures. The author says the video course covers most of the same ground as the book, but the book’s probably a great alternative if you prefer that medium.

Free Programming Basics Course (Ministry of Test)

Cost: Free

Time to complete: a few hours.

Summary: No frills survey of programming concepts and tools. Course material is delivered by web content. There is no feedback or interaction with an instructor.

Programming for Everybody (Getting Started with Python) (Coursera)

Cost: Free 7-day trial, $49/mo after trial ends.

Time to complete: 6 weeks, 2-4 hours/week.

Summary: Long distance entry level college course. Python is the language used to illustrate concepts with videos, web content, and proprietary courseware. Assignments are graded as pass/fail by a auto-grader process.

Programming Foundations with Python (Udacity)

Cost: Free

Time to complete: 6 weeks.

Summary: Self-paced low-level programming course with video instruction, proprietary courseware, discussion forums. Also features quizes and forum interaction for feedback. “Nanodegree” offered for completion of a curriculum group. Favors lecture format with worked examples by the instructor over interactive application of Python by the student.

Master Fundamentals of Programming for Beginners (Udemy)

Cost: $194.00

Time to complete: 13 hours.

Summary: Comprehensive programming course that introduces C and Python. Relies on video lectures and a “Q&A” feature to review and search questions submitted by other students. Little opportunity to write programs and get feedback.

Try Python (Code School)

Cost: $29/mo, but some courses free

Time to complete: 2-3 hours

Summary: Self-paced entry level programming course focused on Python basics. Features videos, slide downloads, proprietary courseware, and an interactive Python emulator.

Learn Python (Code Academy)

Cost: Free, optional upgrades ($19/mo and $199) for access to technical support, more lessons, and additional material.

Time to complete: 10 hours

Summary: Self-paced entry level programming course focused on Python basics. Features web content, proprietary courseware, and an interactive Python emulator.

Ruby in Twenty Minutes (ruby-lang.org)

Cost: Free

Time to complete: 20 minutes

Summary: Very quick “up and running” tutorial. Assumes you have already installed Ruby and are comfortable with the command line. Nothing fancy here – just enough to wet the appetite for the language.

What is Programming? (Khan Academy)

Cost: Free

Time to complete: less than an hour

Summary: Similar to other Kahn Academy lessons, shows you the theory behind programs, and then begins to dig into some Javascript to manipulate images in an interactive emulator. A non-threatening introduction before getting into the deep end of the pool.

Introduction to Computer Science and Programming using Python (Edx/MIT)

Cost: Free. Optional certificate for $49.00, accredited tuition rate of $300. Textbook (available from amazon.com) is not included in the cost.

Time to complete: 9 weeks, 15 hours/week.

Summary: Self-paced college level course featuring introductory computer science concepts. Designed for students not majoring in CS or EE degree programs. Features lectures, interactive assignments, problem sets, and quizes. A certificate of completion is available (see Cost section). Credit hours available for qualified students.

Reactions to “What’s the best programming language to learn for test automation?”

Previously I wrote what I thought about the best programming language to learn for test automation.

Spoilers: I picked Python.

I posted links to that article on a test automation discussion site. People also posted links to the article on social media.

A couple of reactions on Twitter:

https://platform.twitter.com/widgets.js

https://platform.twitter.com/widgets.js

Mark and Brian raise valid counterpoints. Of course, the real answer is always “It depends!”

An expert knows exactly the right tool to select because they have experience to inform that choice. A beginner has almost no experience and their choices are arbitrary.

Sometimes a beginner becomes so overwhelmed by the number of choices, they become paralyzed with indecision. For a beginner, it’s often far more important to get clear direction and get some small victories under their belt. It’s about gaining momentum.

For a beginner, any choice is perfectly fine as long it gets them started. Once a beginner learns one programming language, picking up the next one will be easier.

It’s kind of like the old saying about exercise: “Which exercise is the best? The one you will do.”


Learn To Program Newsletter

I’ll send you the latest posts and useful information to help you learn to program as a test automation engineer.
Email Address

What’s the best programming language to learn for test automation?

People ask this question in other ways, too.

How do you choose a programming language for software testing automation?

Does it matter that I am writing automation code in a different language to that used in development?

How would you rank [insert language here] and what is the most popular language used in building automated frameworks?

In the “real world”, the tool you use to build a test automation tool depends on a lot of things – who are you working with, how much experience do you have with the system under test, is the system a web application or an API, etc.

If you are just starting out then the first thing you must do is to learn how to program.

When you are just starting to learn about programming there are many choices. You are not sure which path is the right path. Everything looks like it is important. You are not sure which way to go or what to do.

You need a clear answer. You need an answer that pushes aside all of the doubt and stress about picking the right thing.

The short answer is: Python.

Python has a lot of advantages for someone learning how to program.

You can write short programs and run them to get quick feedback with Python. Fast feedback means you don’t spend time waiting to see if what you did was right or wrong. You learn and correct mistakes quickly.

Python’s syntax (the rules of how a program is structured) is similar to other programming languages such as Java, and C#. Once you learn the patterns of how a Python program is built, you’ll learn your second language much faster.

Python also enjoys a lot of help for the beginning programmer.

Here are some of the books I recommend:

There are many more out there, but this list should be a good starting point.

Let me know if this was helpful.