Snoopy425
Snoopy425

Reputation: 13

"Not defined" variable (table_count) but I did define it

def is_int(val):
    if type(val) == int:
        return True
    else:
        if val.is_integer():
            return True
        else:
            return False
mult_tble = float(input("Which multiplication table would you like?: "))
if is_int(mult_tble):
    mult_tble = int(mult_tble)
    print(mult_tble)
else:
    print(mult_tble)
def tble():
    table_count = float(input("How high do you want to go?: ")) + 1
    return table_count
tble()
if is_int(table_count):
    table_count = int(table_count)
    print(table_count)
else:
    print("That is not an integer")
    tble()
print("Here's your table:")
for i in range(1, table_count):
    print(i, " x " + str(mult_tble) + " =", i * mult_tble)`

The error message when I run is:

Traceback (most recent call last):
File "C:/Users/embleton/AppData/Local/Programs/Python/Python36-32/game.py", 
line 20, in <module>
if is_int(table_count):
NameError: name 'table_count' is not defined

I thought that I defined table_count in my tble() function, but I guess not. Please help. I'm a noobie at python and I can't figure this out.

Upvotes: 1

Views: 110

Answers (2)

be_good_do_good
be_good_do_good

Reputation: 4441

table_count scope has ended in def tble(). So it errors out saying it is not defined.

So assign the return of tble() to table_count and use it further like below:

table_count = tble()
if is_int(table_count):
    table_count = int(table_count)
    print(table_count)

Upvotes: 1

aquaman
aquaman

Reputation: 1658

You forgot to take the return value of tble().

Try again like this-

def tble():
    table_count = float(input("How high do you want to go?: ")) + 1
    return table_count
table_count = tble()
if is_int(table_count):
    table_count = int(table_count)
    print(table_count)

Upvotes: 2

Related Questions