使用索引

时间:2017-10-05 16:59:59

标签: python

我正在制作一个单词的程序,并制作一个单词阶梯。例如,您开头的单词是cat,您输入要替换的索引,如索引0,然后输入要替换它的字母,例如" h"输出将是" 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()

如您所见,我在get_letter中使用了string.replace函数。问题是当我尝试替换first_word的索引时,我得到错误

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

我已尝试将3行代码连接并在get_index函数调用下方打印字符串,但它不会打印任何内容。

4 个答案:

答案 0 :(得分:2)

您的代码存在许多问题,

  1. 因为您使用的是python2
  2. ,所以请使用raw_input()而不是input()
  3. 名称user_index是get_index()中的局部变量。它在get_letter()中无法访问。将它作为参数传递给get_letter(user_index)。
  4. 您需要分配list_to_string = list_to_string.replace(first_word [user_index],user_letter)。
  5. 这是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()
    

答案 1 :(得分:0)

您已调用变量user_index,但它仅在您定义的函数中定义。您需要将其作为参数传递给另一个函数:

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()

答案 2 :(得分:0)

试试这段代码:

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()

答案 3 :(得分:0)

user_indexget_index函数的局部变量。要解决此错误,您可以:

  1. 使用get_letter(user_index)将其作为参数传递并修改def get_letter(user_index)
    1. 将您的变量声明为global,在global user_index函数中的while True:语句之前添加get_index