Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
Three programs are shown. For each one, name the kind of error that Python will show (NameError or SyntaxError), say which part of the line is wrong, and write the fixed line.
A Print("Hi")
B print("Hi)
C print(Hi)
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. A SyntaxError means that Python cannot read the line. A NameError means that Python can read it, but it does not know a name.
A. Print("Hi"): the line is well formed, but Print is not a name that Python knows, because it is case sensitive. This is a NameError. Fix: print("Hi").
B. print("Hi): the quotation mark is not closed, so Python cannot read the line. This is a SyntaxError. Fix: print("Hi").
C. print(Hi): the line is well formed, but Hi has no quotation marks, so Python looks for a name Hi, and there is none. This is a NameError. Fix: print("Hi").
Check. All three fixes give the same line. The reasons were different.
Answer: A: NameError (capital letter). B: SyntaxError (missing quotation mark). C: NameError (missing quotation marks around the text).
Log in to join the discussion.
Loading…