从用户的回答中列出

making a list from the users answer

我正在做一个项目,我必须根据用户的输入生成一个谜题。我会询问用户他们想要在拼图中包含多少个单词,并且还会询问这个单词。但是,拼图必须包含 4-9 个单词,并且每个单词的长度必须至少为 9 个字母。我已经编写了一些代码作为开始,因为我是分段进行的(我是 python 的新手)但是我的代码不会停止询问用户更多的单词,它只是不断询问。如果用户说他们只想要 4 个词,一旦他们输入了 4 个词,它应该就此停止,但我的代码仍然要求输入更多的词。任何指导表示赞赏!我有 2 python 个文件。在我的 main.py 中,一个名为“main.py”,另一个名为“puzzler.py”,我有以下代码:

import puzzler
changeable_grid = list()

ways = 0
word_list = []

# TODO:
# 1A. ask user to input how many words they wanna work with
def ask_num_words() -> int:
    global num_of_words
    while True:
        try:
            num_of_words = int(input('How many words would you want to work with: '))
        except ValueError as e:
            print(e)
        if puzzler.MIN_WORD <= num_of_words <= puzzler.MAX_WORD:
            break
        else:
            print('Sorry words can only be between {} and {}'.format(puzzler.MIN_WORD, puzzler.MAX_WORD))
            print('')
            continue
    return num_of_words

# 1B. ask user to input the words for the puzzle
    def ask_for_words(amt_of_words: int) -> list:
        global word
        while True:
            try:
                for i in range(amt_of_words):
                    word = input('Please enter the word: ')
                    word_length = len(word)
                    if puzzler.MIN_WORD_LENGTH >= word_length or word_length >= puzzler.MAX_WORD_LENGTH:
                        print('The word must be be between {} and {} in length'.format(puzzler.MIN_WORD_LENGTH,
                                                                                       puzzler.MAX_WORD_LENGTH))
                        print('')
                        word = input('Re-enter the word again:')
                        word_length = len(word)
                word_list.append(word)
            except ValueError as e:
                print(e)
        print(word_list)
        return word_list

if __name__ == "__main__":
    amt_of_words = ask_num_words()
    ask_for_words(amt_of_words

)

到目前为止,在我的 puzzler.py 文件中,我只有以下代码:

MAX_WORD = 9
MIN_WORD = 4
MAX_WORD_LENGTH = 9
MIN_WORD_LENGTH = 1

当您希望某事发生时,您使用“while True:”forever.I认为您应该将其从ask_for_words中删除。