I am working on my first project. I am trying to create a program that can fetch a string from a text file when the number for that string is entered, and allow the user to edit that string if they wish. Right now I'm just trying to get menus out of the way before I learn how to actually implement that.

I'm running into an issue after trying to use recursion to keep a user from entering a bad value for the dealer number.

I get the following traceback when I try to run my code:

Traceback (most recent call last):
  File "C:\Users\Nick\Desktop\AWM Sale Tracking\saletracker.py", line 37, in <module>
    nowviewing()
  File "C:\Users\Nick\Desktop\AWM Sale Tracking\saletracker.py", line 26, in nowviewing
    if is_number(dealer_num) == True:
UnboundLocalError: local variable 'dealer_num' referenced before assignment

My code:

# This system supposedly makes it easier to find current sales than looking it up on a piece of paper like everyone does now

# Let's get all this bull**** out of the way
import os
os.system('cls' if os.name == 'nt' else 'clear') # Gotta get dat cross-compatibility

# Begin prompting user
title = 'AWM Sale Tracking Database v0.1a'
print(title)

def start():
    dealer_num = input('\nPlease enter a dealer number and press ENTER: ')
    return dealer_num

dealer_num = start()

# Check if our input is a number
def is_number(s):
    try:
        int(s)
        return True
    except:
        return False

def nowviewing():
    if is_number(dealer_num) == True:

        # Let user know what the **** they just did
        print('\nNow viewing Dealer ' + dealer_num + '.')
        print('\nWhat would you like to do with this dealer?\n')

    else:
        print('\nInvalid dealer number. Please enter a valid dealer number.')
        dealer_num = start()
        nowviewing()

nowviewing()

# Main dealer menu
print('(1) View current sales\n(2) Edit dealer sale information')
menu_choice = input('\nEnter the number of your choice and press ENTER: ')
print('DEBUG: choice ' + menu_choice + ' selected.')

print('\nEnd of code, exiting.') # Ain't done yet guize

The traceback doesn't make sense, because dealer_num should have already been assigned at line 15. I only have a few days of experience, so I really have absolutely no idea what is wrong.

The problem is that due to the assignment statement at line 34, dealer_num becomes a local variable in nowviewing(). It means that it is a different variable with the same name. The solution is to add the line global dealer_num at the beginning of nowviewing() to tell python that this is a global variable.

Later, you will learn to avoid the need for global statements like this.

Also, the recursive call to nowviewing() at the end of the function would be better written as a while loop, without recursion.

Alright, that makes sense enough.

I still know so little at this point, everything is new to me. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.