Python Forum
How to compare two parameters in a function that has *args?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to compare two parameters in a function that has *args?
#5
(Mar-26-2023, 07:20 PM)deanhystad Wrote: You are checking at the wrong time. Tell the user what they can enter and force them to enter values that are correct.
def get_number(prompt, range=None):
    """Force user to input number.  If range provided, input must be in range."""
    while True:
        try:
            value = int(input(prompt))
            if not range or range[0] <= value <= range[1]:
                return value
            print("Invalid input.  Try again")
        except ValueError:
            print("Invalid input.  Try again")


MAX_LENGTH = 30
MIN_LENGTH = 8
remaining = MAX_LENGTH - 2  # at least 1 lettter, number and special character

special = get_number(f"How many special characters (1-{remaining}): ", (1, remaining))
remaining = remaining - special + 1

if remaining > 2:
    numbers = get_number(f"How many numbers (1-{remaining}): ", (1, remaining))
else:
    numbers = 1

remaining = remaining - numbers + 1
if remaining > 1:
    min_letters = max(1, MIN_LENGTH - (special + numbers))
    letters = get_number(f"How many letters ({min_letters}-{remaining}): ", (min_letters, remaining))
else:
    letters = 1

print(letters, numbers, special)
To answer your question, you could do something like this:
def check(*args):
    """Force all args to be at least 1"""
    return [max(1, arg) for arg in args]

Thank you very much.
Reply


Messages In This Thread
RE: How to compare two parameters in a function that has *args? - by Milan - Mar-26-2023, 07:43 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  function accepts infinite parameters and returns a graph with those values edencthompson 0 883 Jun-10-2022, 03:42 PM
Last Post: edencthompson
  *args implementation and clarification about tuple status amjass12 10 4,135 Jul-07-2021, 10:29 AM
Last Post: amjass12
  [SOLVED] Good way to handle input args? Winfried 2 2,110 May-18-2021, 07:33 PM
Last Post: Winfried
  How can I write a function with three parameters? MehmetAliKarabulut 1 2,450 Mar-04-2021, 10:47 PM
Last Post: Larz60+
  Two Questions, *args and //= beginner721 8 3,583 Feb-01-2021, 09:11 AM
Last Post: buran
  Parameters aren't seen inside function Sancho_Pansa 8 2,979 Oct-27-2020, 07:52 AM
Last Post: Sancho_Pansa
  RuntimeError: Optimal parameters not found: Number of calls to function has reached m bntayfur 0 6,182 Aug-05-2020, 04:41 PM
Last Post: bntayfur
  Function parameters and values as string infobound 1 1,793 Jul-24-2020, 04:28 AM
Last Post: scidam
  does yield support variable args? Skaperen 0 1,704 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,266 Feb-03-2020, 06:03 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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