英文:
CodeHS 8.3.8: Word Ladder not passing the auto grader
问题
在这个CodeHS任务中,你需要创建一个“Word Ladder”程序,用户需要输入一个单词、一个索引和一个字母。程序将替换单词中指定索引位置的字母。如果索引超出范围或字符是大写字母,它应该打印一个错误消息。
这是示例输出:
输入一个单词:cat
输入一个索引(-1退出):1
输入一个字母:o
cot
输入一个索引(-1退出):2
输入一个字母:g
cog
输入一个索引(-1退出):5
无效的索引
输入一个索引(-1退出):-3
无效的索引
输入一个索引(-1退出):0
输入一个字母:L
字符必须是小写字母!
输入一个字母:l
log
输入一个索引(-1退出):-1
这是你编写的代码:
def get_index(word):
while True:
try:
index = int(input("输入一个索引(-1退出):"))
except ValueError:
print("无效的索引!")
continue
if index > len(word) - 1:
print("无效的索引!")
elif index < -1:
print("无效的索引!")
else:
return index
def get_letter():
while True:
letter = input("输入一个字母:")
if len(letter) != 1:
print("必须正好是一个字符!")
continue
elif letter.upper() == letter:
print("字符必须是小写字母!")
continue
return letter
word = input("输入一个单词:")
while True:
index = get_index(word)
if index == -1:
exit(0)
letter = get_letter()
word = word[:index] + letter + word[index+1:]
print(word)
你测试的代码与示例输入一致,但自动评分系统返回了一个错误消息。这可能与CodeHS本身有关,或者你的解决方案不是他们所期望的。
要解决这个问题,你可以尝试以下步骤:
- 仔细检查代码,确保没有拼写错误或语法错误。
- 与其他学生或老师讨论,看看他们是否遇到了类似的问题,以获取更多建议。
- 阅读CodeHS的文档和评分规则,确保你的代码符合他们的要求。
- 尝试将代码中的文本消息与示例输出中的文本消息进行一一比对,确保它们一致。
- 如果仍然无法解决问题,考虑与CodeHS的支持团队联系,他们可能能够提供更多帮助或澄清评分标准。
最重要的是,不要只是复制粘贴其他解决方案,尽力理解问题并尝试修复它。
英文:
> In this CodeHS assignment, you are to create a "Word Ladder", where the the user is supposed to input a word, an index, and a letter. The program will then replace the new letter into the word at that index. If the index is out of range, or the character is uppercase, it should print an error message.
This is the example output they gave:
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
This is the code I wrote:
def get_index(word):
while True:
try:
index = int(input("Enter an index (-1 to quit): "))
except ValueError:
print("Invalid index!")
continue
if index > len(word) - 1:
print("Invalid index!")
elif index < -1:
print("Invalid index!")
else:
return index
def get_letter():
while True:
letter = input("Enter a letter: ")
if len(letter) != 1:
print("Must be exactly one character!")
continue
elif letter.upper() == letter:
print("Character must be a lowercase letter!")
continue
return letter
word = input("Enter a word: ")
while True:
index = get_index(word)
if index == -1:
exit(0)
letter = get_letter()
word = word[:index] + letter + word[index+1:]
print(word)
When I tested the code with the exact input that was given in the example, it worked flawlessly.
However, this is what the auto grader returns:
My output is the exact same, except for a "something is wrong with code" thing added at the bottom of my supposed output. Because of this, the auto grader does not accept it. I think has to do with CodeHS itself, or my solution is just one that they were not expecting.
What can I do to solve this? I don't want to just copy and paste another solution and be done with it.
答案1
得分: 0
I would rewrite the while loop to remove the exit(0) statement as follows:
while True:
index = get_index(word)
if index == -1:
break
letter = get_letter()
word = word[:index] + letter + word[index+1:]
print(word)
英文:
While I have no way to prove this theory, in reviewing your code I would rewrite the while loop to remove the exit(0) statement as follows:
while True:
index = get_index(word)
if index == -1:
break
letter = get_letter()
word = word[:index] + letter + word[index+1:]
print(word)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论