我正在制作一个单词的程序,并制作一个单词阶梯。例如,您开头的单词是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函数调用下方打印字符串,但它不会打印任何内容。
答案 0 :(得分:2)
您的代码存在许多问题,
这是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_index
是get_index
函数的局部变量。要解决此错误,您可以:
get_letter(user_index)
将其作为参数传递并修改def get_letter(user_index)
。 或
global
,在global user_index
函数中的while True:
语句之前添加get_index