Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Each of these three programs stops with an error. Read the last line of each message, and say what the mistake is and how to fix it.
Program A
The message ends with NameError: name 'total_mark' is not defined.
Program B
The message ends with TypeError: can only concatenate str (not "int") to str.
Program C
The message ends with SyntaxError: invalid decimal literal.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. An error message tells you what kind of mistake it is (the word before the colon), and then what is wrong. Read it from the last line.
Program A. A NameError means that Python does not know the name. The program made total_marks but asked for total_mark, without the s. Fix: write print(total_marks).
Program B. A TypeError means that two values of different types were used together in a way that is not allowed. The program joins text with the number age. Fix: change the number to text, and do the sum first: print("Next year you will be " + str(age + 1)). A comma also works: print("Next year you will be", age + 1).
Program C. A SyntaxError means that Python cannot read the line at all. A name cannot start with a digit. Fix: second_prize = "gold".
Check. The three kinds of error are different: an unknown name, the wrong kind of value, and a line that is not valid Python.
Answer: A: a misspelt name. B: text joined to a number. C: a name that starts with a digit.
Log in to join the discussion.
Loading…