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
#1
This is my full program: https://github.com/UnknownBob123/Card-Catalogue


The only thing that is not working as intended, is the minimum and maximum number a user can input, and it is between 1-25. If I input 0, and 26, nothing happens as it continues without stopping - it is supposed to prompt the same window again, asking it to input numbers between 1-25, until then the program won't proceed. I imagine my "validate_value" function is not being called, but I am not fully certain where or why this is happening. I'd appreciate a helping hand on this! :-)

Here is the part where the issue stated above is happening.

def validate_value(value, item): 

    """Input validation: Checks that the user has input a valid value. 

    """ 

    # If the user presses the Cancel button, function is called to  

    # confirm that the user wants to exit. 

    if value == None: 
      
      value = query_cancel() 

 

    # If the user presses the OK button without entering a value, 

    # the no_value() function is called to display an error message 

    # and get input. 

    

    while value == "": 
      
      value = no_value(item) 
      

 

    # The while loop checks that input is valid (from VALUE_MIN to  

    # VALUE_MAX), and if not an error message is displayed, and the  

    # user is prompted to re-enter the value. 

    while float(value) < VALUE_MIN or float(value) > VALUE_MAX: 
      
      
      msg = "Please enter a valid value for " + item + " (from " + str(VALUE_MIN) + " to " + str(VALUE_MAX) + ")." 
      title = "ERROR" 
      value = easygui.enterbox(msg, title) 
      

 

        # If the user pressed OK without entering a value, the function 

        # is called to display and error message and get input. 

      while value == "": 
        value = no_value(item) 

 

        # If the user presses the Cancel button, function is called to  

        # confirm that the user wants to exit. 

      if value == None: 
        
        value = query_cancel()
        
        return (value) 
Reply
#2
The function works as you expect. It keeps asking for a value until I enter a value in the range 1 to 25. I know that you are not calling this function in the code you posted yesterday in the GUI forum. Where should it be used?

Why is there so much whitespace in your program? Are you leaving a blank line after each statement or is this happening when you post the code? If you are doing it on purpose, I don't like it at all. Really makes your code hard to read. Stretches out even short functions so much they don't fit on one screen.

Your indenting is not consistent. Should always be 4 spaces. Your first indent is 4, but subsequent indents are 2 and this combined with the stretched out code makes it tough to see how things line up.
Reply
#3
Apologies for the bad indentations and whitespaces, I will fix these and follow proper PEP-8 Conventions once I can get the program done.

When the window is blank, and I press OK without inputting anything, it does prompt the input window, however when I input 0 OR 26, it still proceeds. I think the "validate_value()" function should be called in the "get_fields()" function, not quite sure how to manipulate that code as I am still pretty much a newbie when it comes to python, and I am not familiar with that code since I did not write it. This is pretty much the only thing that I am trying to implement in my code.
Reply
#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
#5
Awesome! I used the first option to modify get_fields(). You have been extremely helpful to me on my posts, a new programmer just trying to get a grasp of how to manipulate stuff, to build a fundamental base for the future.

I am now cleaning up the code to make it more presentable, (need to work on that haha).

Wish you the best deanhystad, thank you for taking time out of your day to guide people on the forums.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  WHILE LOOP NOT RETURNING USER INPUT AFTER ZerroDivisionError! HELP! ayodele_martins1 7 1,048 Oct-01-2023, 07:36 PM
Last Post: ayodele_martins1
  restrict user input to numerical values MCL169 2 907 Apr-08-2023, 05:40 PM
Last Post: MCL169
  while loop not working-I am using sublime text editor mma_python 4 1,121 Feb-05-2023, 06:26 PM
Last Post: deanhystad
  user input values into list of lists tauros73 3 1,064 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,072 Dec-25-2022, 03:00 PM
Last Post: askfriends
Question Take user input and split files using 7z in python askfriends 2 1,079 Dec-11-2022, 07:39 PM
Last Post: snippsat
  Code won't break While loop or go back to the input? MrKnd94 2 944 Oct-26-2022, 10:10 AM
Last Post: Larz60+
Sad how to validate user input from database johnconar 3 1,906 Sep-11-2022, 12:36 PM
Last Post: ndc85430
  How to split the input taken from user into a single character? mHosseinDS86 3 1,164 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Creating a loop with dynamic variables instead of hardcoded values FugaziRocks 3 1,469 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