Python Forum
Frustrated with Assignment. S.O.S! HELP!
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Frustrated with Assignment. S.O.S! HELP!
#1
Hi, I have this assignment that I am about to be on the verge of giving up. So this assignment calls for creating a function call "def verify(number)", as show below in the code. So the "verify" function is suppose to verify and "pass" or "fail" any given string list in the format of '####-####-####', based on four rules.

These rules are;

-Rule 1; the first digit in the string must be '4'
-Rule 2; the fourth digit must be one greater than the fifth digit (excluding the dash symbol, '-' in the string list)
-Rule 3; the sum of all digits must be evenly divisible by 4
-Rule 4; the first two digits when converted to a two-digit number, and the seventh and eighth digit is also converted to a two digit number, their sum must be equal to 100.

Now, here is my code that I typed up to meet the requirement to meet these four rules:
def verify(number) :

#Rule 1
if number[0] == '4':
  return True
else:
  return False

#Rule 2
without_minus = number[0:4]+number[5:9]+number[10:]
int(without_minus)
for num in without_minus:
  if num[3]+1 > number[4]:
    return True
  else:
    return False

#Rule 3
without_minus = number[0:4]+number[5:9]+number[10:]
int(without_minus)
SumNum = 0
for i in without_minus:
  SumNum = SumNum +1
  if SumNum/4 == 0:
    return True
  else:
    return False

#Rule 4
first_two_digit = number[1] + number[2]
seventh_eighth_digit = number[7] + number[8]
first_two = int(first_two_digit)
seventh_eighth = int(seventh_eighth_digit)
sum = first_two + seventh_eighth
  if sum == 100:
    return True
  else:
    return False

return True # modify this line as needed

input = "4094-3460-2754" # change this as you test your function
output = verify(input) # invoke the method using a test input
print(output) # prints the output of the function
Reply
#2
your function never goes after you check for rule 1. It returns on either line 5 or line 7
if it did - you would see other errors in the code
Although it's OK to short-circuit and return immediately after a rule fails. If it doesn't fail you want to continue and return True after all rules pass.

You can perform all tests in your function or as an alternative and for easier development/tests you may define separate function for each rule. Then you can use these functions in your verify() function
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
@buran where is my incorrect indentation? I don't see it
Reply
#4
the body of the function (lines # 3-40) should be indented one level compared to def line (#1).
probably just a glitch when you post it here
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
Hi, I just want to know how to set my "return" in my function correctly in my code. What do I set my return to if I want to show whether each rule is True or False, based on the test of the four rules for the input? Let me know.

def verify(number):
  #Rule 1
  if int(number[0]) == 4:
    True
    print('passes rule 1')
  else:
    False
    print('violates rule 1')
  #Rule 2
  if (int(number[3]) + 1) >= int(number[5]):
    True
    print('passes rule 1-2')
  else:
    False
    print('passes rule 1, violates rule 2')

  #Rule 3
  SumNum = 0
  num = int(number[0]) + int(number[1]) + int(number[2]) + int(number[3]) + int(number[5]) + int(number[6]) + int(number[7]) + int(number[8]) + int(number[10]) + int(number[11]) + int(number[12]) + int(number[13])
  SumNum = SumNum + num
  if SumNum % 4 == 0:
    True
    print('passes rule 1-3')
  else:
    False
    print('passes rule 1-2, violates rule 3')

  #Rule 4
  first_two_digit = number[0] + number[1]
  seventh_eighth_digit = number[7] + number[8]
  z = int(first_two_digit)
  y = int(seventh_eighth_digit)
  if z + y == 100:
    True
    print('passes rule 1-4')
  else:
    False
    print('passes rule 1-3, violates rule 4')

  return True  # modify this line as needed


input = "4007-6000-0000"
output = verify(input) 
print(output)
Reply
#6
It seems very complicated way with focus on output, not the task at hand.

If numbers may contain something else than numerics one should be defensive against it in the first place:

>>> nums = '123-456-789'
>>> ''.join(n for n in nums if n.isdigit())                                                
'123456789'
However, as there must be calculations performed, it is more convenient to create list of integers:

>>> digits = [int(num) for num in nums if num.isdigit()] 
>>> digits                                                 
[1, 2, 3, 4, 5, 6, 7, 8, 9]
We can return True or False on condition without if-conditional:

>>> digits[0] == 4
False
>>> digit[3] + 1 >= digit[5]
False
We can put all these rules into list and use enumerate() to indicate which rules passed which not. Or use itertools.groupby() if we need present result consecutively.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
Note that a line that is just True or just False does nothing. You need to assign that value or return it or something. Otherwise it just disappears.

Me thinks the simplest way to do this is to check each rule for failure. If it fails, return False. This ends the function, and is called short circuiting the calculation, as buran mentioned. Then at the end, you return True. That will only happen if none of the tests failed.

def validate(text):
    if text[0] != 4: 
        return False
    ...
    return True
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#8
(Nov-17-2019, 12:36 AM)Than999 Wrote: So the "verify" function is suppose to verify and "pass" or "fail" any given string list in the format of '####-####-####', based on four rules.

So the function should return 'pass' or 'fail' for give string, not the compliance to specific rule?

>>> def verify(string):  
...     digits = ''.join(char for char in string if char.isdigit())  
...     first_rule = digits[0] == '4' 
...     second_rule = int(digits[3]) - int(digits[4]) == 1 
...     third_rule = sum(int(num) for num in digits) % 4 == 0 
...     fourth_rule = int(digits[:2]) +  int(digits[6:8]) == 100 
...     if all([first_rule, second_rule, third_rule, fourth_rule]): 
...         return 'pass' 
...     else: 
...         return 'fail' 
...                                                                                       
>>> verify('4094-3460-2754')                                                              
'pass'
>>> verify('4084-3460-2754')                                                              
'fail'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

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