0

I am making a program that is supposed to accept some arguments from a user and check those arguments to see if they satisfy the following conditions: (a) the program receives at least one argument (b) all arguments are integers (c) the arguments are either all odd OR all even (d) return False in all other situations, including the situation in which it is called with no arguments or invalid arguments

I have coded most of it, but I am getting stuck on condition (c). I was trying to add all the arguments into a list and then check to see if all inputs in the list are even or odd, but it isn't quite working. I am not sure what to from here:

def all_odd_or_even(*arguments): # the * means all or everything...
    for arg in arguments:
        if arg.isnumeric() == False and len(arguments) < 1 and not(len(arguments)) % 2 == 0 or not(len(arguments)) %2 != 0:
                    return False  #returns false if these conditions are met 
        return True  #returning true for everything else 
all_odd_or_even()

If anyone can help me out, I'd really appreciate it. Please show the change in code. Thanks in advance!

(Edit): I just changed the code that I had previously, if I could get a second opinion thanks!

  • "and then check to see if all inputs in the list are even or odd" I'm not clear on what part of your code is intended to do this. Is it the `if len(argument_list) % 2 == 0 or len(argument_list) * 2 != 0` part? Hint: what does `len(argument_list)` do by itself? How does this result help to solve the problem? – Karl Knechtel Feb 03 '22 at 23:48
  • Also: how many times should the list be checked? Should it be checked repeatedly after adding each element, or only once after it has been created? Therefore, where should the checking code go? – Karl Knechtel Feb 03 '22 at 23:50
  • Try to think about your *intended approach to the problem* more clearly. Write it out in plain English words, step by step. If you want to check the values as they are added to the list, then think carefully about this question: *under what conditions do you know that you are done checking*? If you find a good value, may you stop the procedure? If you find a bad value, may you stop the procedure? – Karl Knechtel Feb 03 '22 at 23:52
  • `argument.append[argument_list]` should be `argument_list.append(argument)` – Paul M. Feb 03 '22 at 23:52
  • Also, you are potentially prematurely returning `True` from your function. You should only return `True` when you know all three conditions have been satisfied, not when any of them have been satisfied. – Paul M. Feb 03 '22 at 23:53
  • For the third condition, you aren't checking to see if your arguments themselves are even or odd. You are taking the length of `argument_list`, and checking if the length is even or odd. – Paul M. Feb 03 '22 at 23:54
  • Additionally, if the user is expected to enter the string `"stop"` to stop entering arguments, the integer conversion will raise an exception `int(input(...))`. Also, integers don't have a `isnumeric` method. Also, the `elif argument == "stop": break` is redundant, and the `while-else` construct doesn't do what you think it does. – Paul M. Feb 03 '22 at 23:55

0 Answers0