NameError when running code even when it is defined

Question:
I am trying to run a code that asks the user how many iterations they want on the loop and what numbers they want to add. I am fully aware there is a simpler and better way to execute this but when running the code, i noticed that I keep getting a name error even though all the variables i have are defined. Does anyone know why i keep getting name errors?
Repl link:
https://replit.com/@jonathanessombe/Project-1#main.py

sum = 0
count = 1
other_count = 0


while other_count > 0:
    try:
        iter_num = int(input('How many times do you want the iteration to run? '))
    except ValueError:
        print('please enter a valid interger')
        other_count += 1
        continue
    else:
        other_count = 0
else:
    while count <= iter_num:
        try:
            num_input = int(input('Enter an integer: '))
        except ValueError:
            print('please enter a valid integer')
            continue
        else:
            sum += num_input
            count += 1
    else:
        print('The sum of all those number is:', sum)

Before actually going to your repl, just asking something

Why are you using while with else?

By looking at the initial variable definitions, the while loop will not be executed because 0>0 is false?

Also after running your repl (if you can, we appreciate you to provide the error message as well next time as it may include essential informations)

iter_num is not defined because

please fix it by making the variable other_count above 0

total_sum = 0
count = 1
other_count = 1

while other_count > 0:
    try:
        iter_num = int(input('How many times do you want the iteration to run? '))
    except ValueError:
        print('please enter a valid interger')
        other_count += 1
    else:
        other_count = 2 
        break
        

while count <= iter_num:
    try:
        num_input = int(input('Enter an integer: '))
    except ValueError:
        print('please enter a valid integer')
        continue
    else:
        total_sum += num_input
        count += 1
else:
    print('The sum of all those number is:', sum)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.