This FAQ can be read as a standalone article. The Open in Colab button above opens an optional notebook if you want to run the examples, inspect each type, and work through the fixes step by step.
Many beginner errors become less mysterious when you ask two concrete questions: which name is Python reading, and what type of value does that name refer to right now?
Short answer
A NameError means the requested name is unavailable at that moment. Check its exact spelling, whether its assignment ran first, and whether it was created in a different scope.
A TypeError involving text and numbers means the operation received types that do not work together. Remember that input() returns text. Inspect with type(...), decide whether the program means arithmetic or text joining, and then convert deliberately.
Why is my variable undefined?
Symptom: Python reports a message like:
NameError: name 'total_price' is not defined
Likely reason: when Python reached that line, it could not find the exact name total_price in an available scope.
Start with three checks.
Check spelling and capitalization
total_price =15print(total_prcie)
The assignment creates total_price, but the next line asks for total_prcie. Those spellings are different.
Capital letters also matter:
course ="Python"print(Course)
course and Course are different names. A useful repair changes the use to match the assignment:
course ="Python"print(course)
Check assignment order
Python normally runs a script from top to bottom:
print(score)score =10
The print call happens before score receives a value, so it raises NameError. Put the assignment before the use:
score =10print(score)
In a notebook, also check whether the assignment cell actually ran. Text on the page does not create a name until Python executes that cell. See Running Code and Understanding Output for a clean runtime test.
Check where the name was created
A name created inside a function is usually local to that function:
def calculate_total(): total =4+6print(total)calculate_total()print(total)
The function prints 10. The final line then raises NameError because the local name total is not available outside the function.
Return a result when outside code needs it:
def calculate_total(): total =4+6return totalresult = calculate_total()print(result)
TipRead the exact name
Copy the missing name directly from the last line of the traceback. Then search upward for the assignment or definition you expected to create it. The error is evidence about the program’s current state, not a judgment about the learner.
For more traceback practice, continue with Error Messages.
Why did assigning one name produce an unexpected value?
Assignment evaluates the right side first, then makes the name on the left refer to that resulting value.
Predict both output lines before running. The output is:
1210
Line by line:
score = 10 makes score refer to the integer 10.
saved_score = score reads the current value of score, then makes saved_score refer to that same integer value.
score = 12 reassigns score so it now refers to the integer 12.
That reassignment does not rerun the earlier assignment to saved_score.
The statement saved_score = score is not a spreadsheet formula that keeps watching score. Python executes it once when that line runs.
Reassignment moves one name to a new value; it does not replay earlier lines.
flowchart LR
score1["score"] --> ten["10"]
saved["saved_score"] --> ten
reassign["score = 12"] --> score2["score"]
score2 --> twelve["12"]
saved --> ten
Mutable values need one extra check
Lists can be changed in place. Two names can refer to the same list:
original = [2, 4]backup = originaloriginal.append(6)print(original)print(backup)
Both lines print [2, 4, 6]. The assignment backup = original did not create a second list. It gave the existing list another name. The append method then changed that one shared list.
Create a shallow copy when two independent one-level lists are intended:
original = [2, 4]backup = original.copy()original.append(6)print(original)print(backup)
Now original prints [2, 4, 6], while backup prints [2, 4].
NoteTry it
Predict the two outputs for colors = ["blue"], saved = colors, and colors.append("green"). Then replace saved = colors with saved = colors.copy() and compare the evidence.
For deeper practice with mutation and copying, use Lists.
Keyboard input arrives as characters. Python cannot know whether 12 means an age, part of an address, an identifier with leading zeroes, or text that should be joined with other text. Therefore, input() returns a string.
quantity_text =input("How many notebooks? ")print(quantity_text)print(type(quantity_text))
If the learner types 3, the type output is still <class 'str'>.
Convert at the point where the program needs a number:
quantity_text =input("How many notebooks? ")quantity =int(quantity_text)total = quantity *4print("Total:", total)
If the learner types 3, the program prints Total: 12.
Read the conversion line slowly
quantity =int(quantity_text)
Python reads the string stored under quantity_text.
int(...) attempts to create an integer with the same whole-number meaning.
The assignment gives that new integer the name quantity.
The original string still exists under quantity_text.
Not every string describes an integer:
int("three")
This raises ValueError. The operation accepts strings, but this particular string cannot be interpreted as a base-ten integer.
WarningCommon mistake
Do not convert every input automatically. A name, email address, phone number, or postal code is usually text even when it contains digits. Convert only when the program needs numeric behavior such as arithmetic or numeric comparison.
Why can Python not combine a string and a number?
The + symbol has more than one meaning:
print(2+3)print("Py"+"thon")
The first call performs integer addition and prints 5. The second performs string concatenation and prints Python.
This expression is ambiguous, so Python rejects it:
attempts =3print("Attempts: "+ attempts)
Should the program add numerically or join text? Decide the intended result, then write that decision explicitly.
print(value) shows content. print(type(value)) shows the value’s type. Both clues matter: "3" and "three" are strings, but only one can be converted with int(...).
NoteTry it
Fix price_text = "5" and total = price_text + 2 in two different ways: first produce the number 7, then produce the text "52". Explain why the correct conversion depends on the intended result.