简体   繁体   English

CodeHS 8.3.8:Word Ladder 还需要什么?

[英]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 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.当用户为索引输入 -1 时,停止请求输入。

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.您应该有一个函数get_index ,它反复询问用户一个索引,直到他们输入一个在初始字符串可接受的索引范围内的有效整数。 (If they enter a number out of range, you should output invalid index .) (如果他们输入的数字超出范围,您应该输出invalid index 。)
  • You should have another function, get_letter , that repeatedly asks the user for a letter until they enter exactly one lowercase letter.您应该有另一个函数get_letter ,它反复询问用户输入一个字母,直到他们恰好输入一个小写字母。 (If they enter more than one character, you should output Must be exactly one character! . If they enter a capital letter, you should output Character must be a lowercase letter! .) (如果他们输入了多个字符,你应该输出Must be exactly one character!如果他们输入大写字母,你应该输出Character 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.所以我不完全确定如何为所有大写字母制作if / else语句并且只允许一个字母。 I'm also not sure about what I need to put in my for loop to make it end when I enter -1 .我也不确定当我输入-1时我需要在我for循环中放入什么以使其结束。

Word Ladder should keep running and keep printing the results until the index is -1. Word Ladder 应继续运行并继续打印结果,直到索引为 -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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM