Python Forum
WHILE Loop - constant variables NOT working with user input boundaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
WHILE Loop - constant variables NOT working with user input boundaries
#4
Modify get_fields to do the checking.
def get_fields(msg, title, fields):
    """Get values for multiple fields.  Returns values in dictionry"""
    values = easygui.multenterbox(msg, title, fields)
  
    # Validate field values
    while True:
        missing_fields = [field for field, value in zip(fields, values) if not value.strip()]
        invalid_values = []
        for field, value in zip(fields, values):
            try:
                value = int(value)
                if not VALUE_MIN <= value <= VALUE_MAX:
                    invalid_values.append(field)  # Not in range
            except ValueError:
                invalid_values.append(field)   # Not a number

        if missing_fields:
            missing_fields = ", ".join(missing_fields)
            values = easygui.multenterbox(f"Enter values for {missing_fields}", title, fields, values)
        elif invalid_values:
            invalid_values = ", ".join(invalid_values)
            values = easygui.multenterbox(f"{invalid_values} must be in range {VALUE_MIN}..{VALUE_MAX}", title, fields, values)
        else:
            return {field:value for field, value in zip(fields, values)}
If you'd rather use the validate function you would call that in the add_combo() function.
def add_combo(): 
    '''Add new combo to menu''' 
    while True:
        combo_name = easygui.enterbox(
            "Enter Monster Card name: ",
            title = "Monster Card Name").capitalize()
  
        if check_exists(combo_name): 
            easygui.msgbox("That card name has already been used.") 
        else:
            card_catalogue[combo_name] = get_fields(
                msg="Enter Values",
                title="Monster Card Value",
                fields=("Strength", "Speed", "Stealth", "Cunning"))

            for field, value in card_catalogue[combo_name].items():
                validate_value(value, field)
    
        if not confirm("Do you want to add another monster card?"):
            break
Reply


Messages In This Thread
RE: WHILE Loop - constant variables NOT working with user input boundaries - by deanhystad - Apr-05-2022, 06:12 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,190 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 1,007 Apr-08-2023, 05:40 PM
Last Post: MCL169
  while loop not working-I am using sublime text editor mma_python 4 1,231 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,151 Dec-29-2022, 05:54 PM
Last Post: deanhystad
Information How to take url in telegram bot user input and put it as an argument in a function? askfriends 0 1,190 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,185 Dec-11-2022, 07:39 PM
Last Post: snippsat
  Code won't break While loop or go back to the input? MrKnd94 2 1,058 Oct-26-2022, 10:10 AM
Last Post: Larz60+
Sad how to validate user input from database johnconar 3 2,062 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,259 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,574 Jul-27-2022, 08:50 PM
Last Post: rob101

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020