Back

Debug Challenge: Runtime & Casting

Difficulty  

divider

Objective

Four more broken programs. This time they all involve input and casting, which means three of them crash while running rather than refusing to start.

Activity 1.8 named the three kinds of mistakes. Use those names here.

Skills to Practice

  • Reading a traceback
  • Telling a runtime error from a logic error
  • Knowing when and what to cast

The Three Kinds, Again

  • Syntax error — Python cannot read it. Nothing runs.
  • Runtime error — it starts, then crashes partway through. You get a traceback.
  • Logic error — it runs all the way through and gives you the wrong answer, with no error at all.

One of the four below is a logic error. Deciding which one is most of the work.


The Four Programs

Run each one, then write down which kind of mistake it is, what is actually wrong, and the corrected line.


Bug 1

Symptom: enter 16 and it crashes. The traceback ends with a TypeError.

Bug 1
age = input("How old are you? ")
print("Next year you will be " + age + 1)

Careful — there are two + signs on that line, and they are not doing the same job. Which one is the problem?


Bug 2

Symptom: enter 20 and it works fine. Enter 12.99 and it crashes with a ValueError.

Bug 2
price = int(input("What did it cost? "))
print("With tax:", price * 1.07)

A program that only breaks on some inputs is still broken. What kind of number is a price?


Bug 3

Symptom: works for most inputs. Enter 0 players and it crashes with a ZeroDivisionError.

Bug 3
total = int(input("Total points scored: "))
players = int(input("How many players? "))
print("Points each:", total / players)

This one is different from the others: the casting is correct and the math is correct. Describe in words what the program would have to check before dividing. You do not have the tool to fix it properly yet — that arrives in Activity 1.10.


Bug 4

Symptom: enter 3 and 5. It prints Sum: 35. No crash, no error message, and the answer should be 8.

Bug 4
first = input("First number: ")
second = input("Second number: ")
print("Sum:", first + second)

Explain why it printed 35 rather than 8. The program did exactly what it was told — that is what makes this kind of bug dangerous.


Submit

For each of the four bugs, submit:

  • Which kind of mistake it is — syntax, runtime, or logic
  • One sentence on what was wrong
  • The corrected line, or for Bug 3 a description of the fix

Bugs 1, 2, and 3 all handed you a traceback. Bug 4 handed you nothing. Which one would you have been least likely to notice on your own?

Commence Debugging