简体   繁体   中英

Validate That A Sequence of Numbers Meets Certain Criteria in Python

I am 1.5 weeks into learning Python and working through one of my first assignments. I have an input string in Python that looks like the following:

input = "4567-8901-2345"

Against this string I want to check for four rules:

  1. The first digit must be a 4.
  2. The fourth digit must be one greater than the fifth digit.
  3. The sum of all digits must be evenly divisible by 4.
  4. If you treat the first two digits as a two-digit number, and the seventh and eighth digits as a two-digit number, their sum must be 100.

I have written the following code. Each section of the code appears to meet the 4 rules individually, but the challenge I am having is that once rule 1 is successfully met for example, the code stops running - it should check each rule in succession, and once a rule fails, it should print out the rule number that wasn't met. What am I missing?

def verify(number) :

#rule 1
  value_first = input[0]
  number = int(value_first)
  if number == 4 :
    return True
  else :
    return ('1')

#rule 2
  value_second = input[3]
  value_third = input[5]
  number_second = int(value_second)
  number_third = int(value_third)
  if number_second == number_third + 1 :
    return True
  else :
    return ('2')

#rule3
  total = sum(int(x) for x in input if x.isdigit())
  if total % 4 == 0 :
    return True
  else :
    return ('3')

#rule4
  pair_one = input[0] + input[1]
  pair_two = input[7] + input[8]
  pair_one_int = int(pair_one)
  pair_two_int = int(pair_two)
  if pair_one_int + pair_two_int == 100 :
    return True
  else :
    return ('4')

input = "4567-8901-2345"
output = verify(input)
print(output)  

To expand on my comment, I've split each rule into a function of its own here; each function returns True if they pass or False if they don't:

def check_rule1(input):
    return input[0] == "4"


def check_rule2(input):
    value_second = input[3]
    value_third = input[5]
    number_second = int(value_second)
    number_third = int(value_third)
    return number_second == number_third + 1


def check_rule3(input):
    total = sum(int(x) for x in input if x.isdigit())
    return total % 4 == 0


def check_rule4(input):
    pair_one = input[0] + input[1]
    pair_two = input[7] + input[8]
    pair_one_int = int(pair_one)
    pair_two_int = int(pair_two)
    return (pair_one_int + pair_two_int == 100)


def check_rules(input):
    print(input)
    print("  Rule 1:", check_rule1(input))
    print("  Rule 2:", check_rule2(input))
    print("  Rule 3:", check_rule3(input))
    print("  Rule 4:", check_rule4(input))


check_rules("4567-8901-2345")

This will output

4567-8901-2345
  Rule 1: True
  Rule 2: False
  Rule 3: False
  Rule 4: False

– making check_rules call each check function and return the number of the rule that fails is left as an exercise to the reader.

Either, as AKX says put each rule in a single function, this will help you to make more sense of it all or don't return true. This way, whenever a condition fails the validation is stopped and the rule number returned and only if all validations are correct it returns true at the very end once.

def verify(number) :

#rule 1
  value_first = input[0]
  number = int(value_first)
  if number != 4 :
    return ('1')

#rule 2
  value_second = input[3]
  value_third = input[5]
  number_second = int(value_second)
  number_third = int(value_third)
  if number_second != number_third + 1 :
    return ('2')

#rule3
  total = sum(int(x) for x in input if x.isdigit())
  if total % 4 != 0 :
    return ('3')

#rule4
  pair_one = input[0] + input[1]
  pair_two = input[7] + input[8]
  pair_one_int = int(pair_one)
  pair_two_int = int(pair_two)
  if pair_one_int + pair_two_int != 100 :
    return ('4')
  return True

input = "4567-8901-2345"
output = verify(input)
print(output)  

I would put each validation inside its own function. Your actual code ignores the 2, 3 and 4 rules because you are returning a value (True or 1) within your first if/else codeblock.

If you don't want to use functions, store the result of each evaluation inside a list/tuple and check if every single element of that list/tuple is True in order to take for granted that the input is a valid string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM