简体   繁体   中英

Encountering a name error while using an index

I'm making a program that takes a word, and makes a word ladder. For example the word you start with is cat, you enter the index you want to replace, like index 0, then enter the letter you want to replace it with, like "h" and the output will be "hat"

first_word = input("Enter a word: ")

my_list = [first_word]

def get_letter():
    while True:
        user_letter = input("Enter a letter: ")
        if len(user_letter) > 1:
            print "Must be exactly one character!"
            continue
        if user_letter.isupper():
            print "Character must be a lowercase letter!"
            continue
        else:
            list_to_string = "".join(my_list)
            list_to_string.replace(first_word[user_index], user_letter)
            print list_to_string
            break

def get_index():
    while True:
        user_index = int(input("Enter an index(-1 to quit): "))

        if user_index == -1:
            break
        elif user_index > len(first_word):
            print "Invalid index"
        elif user_index < -1:
            print "Invalid index"
        else:
            get_letter()



get_index()

As you can see, I'm using the string.replace function inside the get_letter. The problem is that when I try to replace the index of first_word, I get the error

NameError: name 'user_index' is not defined on line 17

I've tried putting the 3 lines of code that join and print the string below the get_index function call, but it doesn't print anything.

There are a number of issues with your code,

  1. Use raw_input() instead of input() since you are using python2
  2. The name user_index is a local variable in get_index(). It is not accessible in get_letter(). Pass it as a parameter to get_letter(user_index).
  3. You need to assign list_to_string = list_to_string.replace(first_word[user_index], user_letter).

Here is the running program in python2,

first_word = raw_input("Enter a word: ")
my_list = [first_word]
print(my_list)
def get_letter(user_index):
    while True:
        user_letter = raw_input("Enter a letter: ")
        if len(user_letter) > 1:
            print "Must be exactly one character!"
            continue
        if user_letter.isupper():
            print "Character must be a lowercase letter!"
            continue
        else:
            list_to_string = "".join(my_list)
            list_to_string = list_to_string.replace(first_word[user_index], user_letter)
            print list_to_string
            break

def get_index():
    while True:
        user_index = int(raw_input("Enter an index(-1 to quit): "))

        if user_index == -1:
            break
        elif user_index > len(first_word):
            print "Invalid index"
        elif user_index < -1:
            print "Invalid index"
        else:
            get_letter(user_index)



get_index()

You have called your variable user_index but but it is only defined in the function that you defined it in. You need to pass it as an argument to the other function:

first_word = input("Enter a word: ")

my_list = [first_word]

def get_letter(user_index):
    while True:
        user_letter = input("Enter a letter: ")
        if len(user_letter) > 1:
            print "Must be exactly one character!"
            continue
        if user_letter.isupper():
            print "Character must be a lowercase letter!"
            continue
        else:
            list_to_string = "".join(my_list)
            list_to_string.replace(first_word[user_index], user_letter)
            print list_to_string
            break

def get_index():
    while True:
        user_index = int(input("Enter an index(-1 to quit): "))

        if user_index == -1:
            break
        elif user_index > len(first_word):
            print "Invalid index"
        elif user_index < -1:
            print "Invalid index"
        else:
            get_letter(user_index)



get_index()

Try this code:

first_word = input("Enter a word: ")

my_list = [first_word]

def get_letter():
    while True:
        index_value=int(" ".join(str(x) for x in track_index))


        user_letter = input("Enter a letter: ")
        if len(user_letter) > 1:
            print ("Must be exactly one character!")
            continue
        if user_letter.isupper():
            print ("Character must be a lowercase letter!")
            continue
        else:
            list_to_string = "".join(my_list)

            print(list_to_string.replace(first_word[index_value], user_letter))

            break
track_index=[]
def get_index():
    while True:
        user_index = int(input("Enter an index(-1 to quit): "))

        if user_index == -1:
            break
        elif user_index > len(first_word):
            print ("Invalid index")
        elif user_index < -1:
            print ("Invalid index")
        else:
            track_index.append(user_index)
            get_letter()




get_index()

user_index is a local variable to the get_index function. To resolve the error you can either:

  1. Pass it as a parameter using get_letter(user_index) and modifying def get_letter(user_index) .

or

  1. Declare your variable as global , add global user_index before your while True: statement in the get_index function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM