![](/img/trans.png)
[英]Why doesn't my code for CodeHS 8.3.8: Word Ladder pass the autograder?
[英]What else do I need for CodeHS 8.3.8: Word Ladder?
這是我應該做的:
你的朋友想嘗試做一個字梯! 這是一個單詞列表,其中每個單詞與之前的單詞有一個字母的差異。 這是一個例子:
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
語句並且只允許一個字母。 我也不確定當我輸入-1
時我需要在我for
循環中放入什么以使其結束。
Word Ladder 應繼續運行並繼續打印結果,直到索引為 -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)
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))
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 character!")
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))
請記住,您可以像這樣在變量中調用函數:
returned_index = get_index()
這是正確的代碼:
user_string = input("Enter a string: ")
def get_index():
while True:
try:
user_index = int(input("Enter an Index: "))
if user_index >= len(user_string):
print("Invalid Index")
continue
if user_index < -1:
print("Invalid Index")
continue
except ValueError:
print("Invalid Index")
continue
return user_index
def get_string():
while True:
user_letter = input("Enter letter: ")
if user_letter.isupper():
print("Character must be a lowercase letter!")
continue
if len(user_letter) > 1:
print("Must be exactly one character!")
continue
return user_letter
while True:
user_string = list(user_string)
returned_index = get_index()
if returned_index == -1:
break
returned_string = get_string()
user_string[returned_index] = returned_string
print("".join(user_string))
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.