Home > OS >  Python: For specific items in a dictionary, continue prompting for input until input is of correct f
Python: For specific items in a dictionary, continue prompting for input until input is of correct f

Time:04-07

I want my code to prompt the user to enter book details, but if they don't enter either a positive whole/decimal number or "N/A" for the rating and # of pages, they will be re-prompted until they do so. The user may also quit the program at any input prompt by entering "Q" as input. At the end, add_details_dict will be filled out instead of having None as all the keys. Here's what I have so far:

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

The problem is if the user doesn't enter a whole number, decimal number, or 'N/A' for the rating/pages. After informing the user that the rating/pages must be a whole number, decimal number, or 'N/A', I want the program to go back to the beginning of the current iteration of the for loop, the prompting line, which is current_input = input(prompt). So it will re-prompt them for the rating/pages, and if they mess up again, it will keep re-prompting until both the rating and the pages are either whole numbers, decimal numbers, or 'N/A'. I want the fixed code to output this:

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

Then for add_details_dict, I should get the following:

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

I appreciate a fix for this problem so that the program satisfies the above results.

CodePudding user response:

If you add a loop in for each key and only break when the user enters a good value, then the prompt will keep coming up until they enter something acceptable. Of course then the break if they enter Q won't work, so you can use a boolean quit variable for that:

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
  • Related