NameError: name 'Steps_in_bisection_search' is not defined but i already defined

#inputs :
annual_salary=float(input("Enter your annual salary: "))
portion_saved=float(input("Enter the percent of your salary to save, as a decimal: "))
#variables :
total_cost=1000000
semi_annual_raise=0.7
current_savings=0
portion_down_payment=0.25*total_cost
r=0.04
annual_payment=current_savings*r/12
monthly_savings=(annual_salary / 12)*portion_saved
portion_saved = portion_saved * monthly_savings 
number_of_months=0
low = 0
high = portion_down_payment
guess = (high + low)/2.0
epsilon=100
Steps_in_bisection_search: 0 
#a program to calculate how many months it will take someone to save up enough money for a down payment in 36 months :
while  (guess - portion_down_payment) >= epsilon: 
  if guess < portion_down_payment :
    low = guess
  else:
    high = guess
  guess = (high + low)/2.0
  if number_of_months % 6 == 0 and number_of_months != 0:
    monthly_savings += monthly_savings*semi_annual_raise
  current_savings += monthly_savings + ((current_savings * 0.04) / 12) 
  Steps_in_bisection_search +=1
  number_of_months += 1
print(f"Steps in bisection search: {Steps_in_bisection_search}")

i have already searched about this error , but unfortunately i didn’t find any suitable solution.
thanks in advance.

What does this line do?

Steps_in_bisection_search: 0

You have that line twice.

If you introduce this line:

print(guess - portion_down_payment, epsilon)

… just ahead of the while: block, you may be able to see the issue.

I didn’t notice that i wrote that line 2 times,well now i corrected and i did what you said but i haven’t noticed any changes.
the problem is that Steps_in_bisection_search is not defined and unbound,but i sure that i defined it by writing this line : (Steps_in_bisection_search: 0)
but the error remains :
File “c:\Users\hp\Desktop\PYTHON\mit3.py”, line 32, in
print(f"Steps in bisection search: {Steps_in_bisection_search}")
^^^^^^^^^^^^^^^^^^^^^^^^^
NameError: name ‘Steps_in_bisection_search’ is not defined

Okay, but I still fail to understand what that code line does.

Introduce the “print debug” line that I posted and you’ll see that the while: block will never execute.

If you want a type annonation to say that Steps_in_bisection_search is an int, you say:

Steps_in_bisection_search: int

If you want to assign 0 to Steps_in_bisection_search, you say:

Steps_in_bisection_search = 0

What you actually wrote was a type annotation, saying that Steps_in_bisection_search is of type 0.

1 Like

oh ,thanks.
i found the solution, i added abs() to the condition to make it true

i did that because i wanted to know the exact number of steps that it can make by adding this line number_of_months += 1.

This does not actually create a definition. We can test that at the REPL:

>>> x:0
>>> x
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'x' is not defined

Like on all the lines that do work, in Python we need = or similar to set a value. There is no such thing as declaring variables; assigning to them the first time is essentially the same as every other time (there just doesn’t happen to be an existing value to replace).

Instead, Steps_in_bisection_search: 0 creates a type annotation that third-party tools can use to try type-checking the code ahead of time. (Except that, for this to work properly, the annotation should use a type like int, rather than a value like 0.) But it has no meaning when the code is actually running.

If you want to make Steps_in_bisection_search have the value 0, that looks like Steps_in_bisection_search = 0.

1 Like

THANKS Karl!!!
i didn’t notice it,it is such a silly mistake.