这是我应该做的:
您的朋友想尝试制作单词阶梯!这是单词列表,其中每个单词与之前的单词有一个字母的差异。这是一个示例:
cat
cot
cog
log
编写一个程序来帮助您的朋友。它应该执行以下操作:
这是幕后应该发生的事情:
get_index
,该函数反复要求用户提供索引,直到他们输入的有效整数在初始字符串的索引可接受范围内。 (如果他们输入的数字超出范围,则应输出invalid index
。)get_letter
,该功能反复要求用户输入一个字母,直到他们输入一个完全小写的字母为止。 (如果他们输入多个字符,则应输出Must be exactly one character!
。如果他们输入大写字母,则应输出Character must be a lowercase letter!
。)这是程序的示例运行:
Enter a word: cat
Enter an index (-1 to quit): 1
Enter a letter: o
cot
Enter an index (-1 to quit): 2
Enter a letter: g
cog
Enter an index (-1 to quit): 5
Invalid index
Enter an index (-1 to quit): -3
Invalid index
Enter an index (-1 to quit): 0
Enter a letter: L
Character must be a lowercase letter!
Enter a letter: l
log
Enter an index (-1 to quit): -1
这是我现在的代码:
word = input("Enter a word: ")
for i in range():
get_index = int(input("Enter an index (-1 to quit): "))
if get_index < -1:
print "Invalid index"
elif get_index > 3:
print "Invalid index"
else:
letter = input("Enter a letter: ")
word = word[:get_index] + letter + word[get_index + 1:]
print word
因此,我不太确定如何对所有大写字母进行if
/ else
声明,并且只允许一个字母。我也不确定在输入for
时需要在-1
循环中添加什么以使其结束。
答案 0 :(得分:0)
梯形图应继续运行并继续打印结果,直到索引为-1。这是一个快速的解决方案。
def get_index(word):
while True:
try:
pos = int(input("Enter an index: "))
if pos == -1:
return pos
elif pos >= len(word):
print "invalid index"
elif pos <= -1:
print "invalid index"
else:
return pos
except ValueError:
print "invalid index"
def get_letter():
while True:
char = str(input("Enter a letter: "))
if char.islower() and len(char)==1:
return char
elif not char.islower():
print "Character must be a lowercase letter!"
elif len(char) > 1:
print "Must be exactly one character!"
def word_ladder(word):
while True:
pos = get_index(word)
if pos == -1:
return
else:
char=get_letter()
word = list(word)
word[pos] = char
word = ("").join(word)
print word
word = input("Enter a word: ")
word_ladder(word)
答案 1 :(得分:0)
init_input = input("Enter word: ")
def get_index():
while True:
try:
index = int(input("Index (-1 to quit): "))
except ValueError:
print("This must be a number!")
continue
if index < 0 and index != -1 or index > len(init_input)-1:
print("Invalid Index!")
else:
return(index)
def get_letter():
while True:
letter = input("Enter a letter: ")
if letter != letter.lower():
print("Character must be a lowercase letter!")
elif len(letter) != 1:
print("Must be exactly one letter!")
else:
return(letter)
while True:
init_input = list(init_input)
index = get_index()
if index == -1:
break
char = get_letter()
init_input[index] = char
print(''.join(init_input))
答案 2 :(得分:0)
MAIL_USERNAME=example@example.com
答案 3 :(得分:-1)
word = str(input("your word: "))
print(word)
run = True
while run:
#ensure he enters a number not letter
while True:
try:
index = int(input("enter index: "))
break
except:
print("please enter a number not letter")
#check to see if the index is within the provided word lenght
while -1 < index < len(word):
#doesn't matter if he enters an uppercase letter becasue the lowermethod will turn it to lowercase
letter = str(input("enter letter: ")).lower()
#check to ensure he provides one letter only
while len(letter) == 1:
word = word[:index] + letter + word[index + 1 :]
print(word)
break
else:
print("enter one letter")
break
else:
#quits if the index is -1
if index == -1:
run = False
#if the index not -1 not in the length of the word ,prints invalid
else:
print("invalid index")
编辑了我犯的错误并对其进行了测试,现在可以正常工作。 预期输出:
your word: cat
cat
enter index: 1
enter letter: o
cot
enter index: -3
invalid index
enter index: 5
invalid index
enter index: -1