根据用户的回答制作列表



我正在做一个项目,我必须根据用户的输入生成一个谜题。我会问用户他们想在拼图中包含多少单词,并询问这个单词。然而,这个谜题必须有4-9个单词,每个单词必须至少有9个字母长。我已经写了一些代码来开始,因为我是分块写的(我是python的新手(,但我的代码并没有停止向用户询问更多的单词,它只是不断地询问。如果用户说他们只想要4个单词,并且一旦他们输入了4个单词时,它应该停止,但我的代码仍然要求更多的单词。感谢您的指导!我有两个python文件。一个名为";main.py";另一个被命名为";puzzler.py";在我的main.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

您使用"而True:";当你希望某件事永远发生的时候。我认为你应该把它从ask_for_words中取出来。

相关内容

  • 没有找到相关文章

最新更新