Python: 对于字典中的特定条目,继续提示输入直到输入格式正确

Python: For specific items in a dictionary, continue prompting for input until input is of correct form

我希望我的代码提示用户输入图书详细信息,但如果他们没有输入正数 whole/decimal 或“N/A”作为评分和页数,他们将被重新提示,直到他们这样做。用户也可以在任何输入提示下通过输入“Q”作为输入来退出程序。最后,将填写 add_details_dict 而不是 None 作为所有键。这是我目前所拥有的:

add_details_dict = {'title':None, 'author':None, 'language':None, 'publisher':None, 'category':None, 'rating':None, 'pages':None}
for key in add_details_dict.keys():
    prompt = "Enter the " + key + " of the book you want to add. To quit: Q)uit\n"
    current_input = input(prompt)
    if current_input == 'Q':
        break
    else:
        if key in ['rating', 'pages'] and current_input != 'N/A':
            try:
                add_details_dict[key] = float(current_input)
            except ValueError:
                print ("The ", key, " must be a positive and a whole number, decimal number, or 'N/A'.", sep='')
        else:
            add_details_dict[key] = current_input

问题是如果用户没有为 rating/pages 输入整数、小数或 'N/A'。在通知用户 rating/pages 必须是整数、小数或 'N/A' 后,我希望程序回到 for 循环当前迭代的开头,即提示行,这是 current_input = 输入(提示)。所以它将重新提示他们输入 rating/pages,如果他们再次搞砸了,它将继续重新提示,直到评级和页面都是整数、小数或 'N/A'。我想要固定代码输出这个:

Enter the title of the book you want to add. To quit: Q)uit:
sometitle
Enter the author of the book you want to add. To quit: Q)uit:
someauthor
Enter the language of the book you want to add. To quit: Q)uit:
English
Enter the publisher of the book you want to add. To quit: Q)uit:
somepublisher
Enter the category of the book you want to add. To quit: Q)uit:
somecategory
Enter the rating of the book you want to add. To quit: Q)uit:
nonsenserating
The rating must be a positive and a whole number, decimal number, or 'N/A'.
Enter the rating of the book you want to add. To quit: Q)uit:
5.7
Enter the pages of the book you want to add. To quit: Q)uit:
nonsensepages
The pages must be a positive and a whole number, decimal number, or 'N/A'.
Enter the pages of the book you want to add. To quit: Q)uit:
557

然后对于 add_details_dict,我应该得到以下信息:

add_details_dict = {'title':'sometitle', 'author':'someauthor', 'language':'English', 'publisher':'somepublisher', 'category':'somecategory', 'rating':5.7, 'pages':557}

感谢解决此问题,使程序满足上述结果。

如果您为每个键添加一个循环,并且仅当用户输入合适的值时 break,那么提示将不断出现,直到他们输入可以接受的值。 当然,如果他们输入 Q,break 将不起作用,因此您可以为此使用布尔 quit 变量:

add_details_dict = {'title':None, 'author':None, 'language':None, 'publisher':None, 'category':None, 'rating':None, 'pages':None}
quit = False
for key in add_details_dict.keys():
    while True:
        prompt = "Enter the " + key + " of the book you want to add. To quit: Q)uit\n"
        current_input = input(prompt)
        if current_input == 'Q':
            quit = True
            break
        else:
            if key in ['rating', 'pages'] and current_input != 'N/A':
                try:
                    add_details_dict[key] = float(current_input)
                    break
                except ValueError:
                    print ("The ", key, " must be a positive and a whole number, decimal number, or 'N/A'.", sep='')
            else:
                add_details_dict[key] = current_input
                break
    if quit:
        break