What Else Do I Need for Codehs 8.3.8: Word Ladder?
This Is What I Am Supposed to Do: Your Friend Wants to Try to Make a Word Ladder! This Is a List of Words Where Each Word Has a One-Letter Difference from the...
This is what I am supposed to do:
Your friend wants to try to make a word ladder! This is a list of words where each word has a one-letter difference from the word before it. Here’s an example:
cat
cot
cog
log
Write a program to help your friend. It should do the following:
- Ask your friend for an initial word.
- Repeatedly ask them for an index and a letter.
- You should replace the letter at the index they provided with the letter they enter.
- You should then print the new word.
- Stop asking for input when the user enters -1 for the index.
Here’s what should be happening behind the scenes:
- You should have a function,
get_index, that repeatedly asks the user for an index until they enter a valid integer that is within the acceptable range of indices for the initial string. (If they enter a number out of range, you should outputinvalid index.) - You should have another function,
get_letter, that repeatedly asks the user for a letter until they enter exactly one lowercase letter. (If they enter more than one character, you should outputMust be exactly one character!. If they enter a capital letter, you should outputCharacter must be a lowercase letter!.) - You should store a list version of the current word in a variable. This is what you should update each time the user swaps out a new letter.
- Each time you have to print the current word, print the string version of the list you are keeping in your variable.
Here’s what an example run of your program might look like:
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 my code right now:
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
So I'm not entirely sure how to make an if/else statement for all capital letters and only allow one letter. I'm also not sure about what I need to put in my for loop to make it end when I enter -1.
5 Answers
Word Ladder should keep running and keep printing the results until the index is -1. Here is a quick solution.
play word ladder
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))
Remember that you can call a function in a variable like this:
returned_index = get_index()
Here is the correct code:
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")
edited the mistakes i made and tested it, works fine now. expected output:
your word: cat
cat
enter index: 1
enter letter: o
cot
enter index: -3
invalid index
enter index: 5
invalid index
enter index: -1