can you explain this logically and properly i have attention deficit disorder - Solving…
- User
can you explain this logically and properly i have attention deficit disorder - Solving the Problem With Boolean operators at the ready, we can tackle the Telemarketers problem. Our solution is in Listing 2-2. num1 = int(input()) num2 = int(input()) num3 = int(input()) num4 = int(input()) ❶ if ((num1 == 8 or num1 == 9) and (num4 == 8 or num4 == 9) and (num2 == num3)): print('ignore') else: print('answer') Listing 2-2: Solving Telemarketers As in Winning Team, we start by reading the input and converting it to integers. The high-level structure of our if statement ❶ is three expressions connected by and operators; each of them must be True for the entire expression to be True. We require that the first number be 8 or 9, that the fourth number be 8 or 9, and that the second and third numbers be equal. If all three of these conditions hold, then we know that the phone number belongs to a telemarketer, and we output ignore. Otherwise, the phone number does not belong to a telemarketer, and we output answer. I’ve split the Boolean expression over three lines. This requires wrapping the entire expression in an additional pair of parentheses, as I have done. (Without those parentheses, you’ll get a syntax error, because there’s no indication to Python that the expression is continuing on the next line.) Python style guides suggest that a line be no longer than 79 characters. A line with the full Boolean expression would squeak in there at 76 characters. But I think the three-line version is clearer, highlighting each condition that must be True on its own line. We have a good solution here. To explore a little further, let’s discuss some alternate approaches. Our code uses a Boolean expression to detect when a phone number belongs to a telemarketer. We could have also chosen to write code that detects when a phone number does not belong to a telemarketer. If the phone number doesn’t belong to a telemarketer, we should output answer; otherwise, we should output ignore. If the first digit isn’t 8 and isn’t 9, then the phone number doesn’t belong to a telemarketer. Or, if the fourth digit isn’t 8 and isn’t 9, then the phone number doesn’t belong to a telemarketer. Or, if the second and third digits aren’t equal, then the phone number doesn’t belong to a telemarketer. If even one of these expressions is True, then the phone number doesn’t belong to a telemarketer. See Listing 2-3 for a version of the code that captures this logic. num1 = int(input()) num2 = int(input()) num3 = int(input()) num4 = int(input()) if ((num1 != 8 and num1 != 9) or (num4 != 8 and num4 != 9) or (num2 != num3)): print('answer') else: print('ignore') Listing 2-3: Solving Telemarketers, alternate approach It’s not easy getting all of those !=, or, and and operators correct! Notice, for example, that we’ve had to change all == operators to !=, all or operators to and, and all and operators to or. An alternate approach is to use the not operator to negate the “is a telemarketer” expression in one shot. See Listing 2-4 for that code. num1 = int(input()) num2 = int(input()) num3 = int(input()) num4 = int(input()) if not ((num1 == 8 or num1 == 9) and (num4 == 8 or num4 == 9) and (num2 == num3)): print('answer') else: print('ignore') Listing 2-4: Solving Telemarketers, not operator Which of these solutions do you find most intuitive? There’s often more than one way to structure the logic of an if statement, and we should use the one that’s easiest to get right. To me, Listing 2-2 is the most natural, but you may feel otherwise! Choose your favorite version and submit it to the judge. You should see that all test cases pass.
- Assistant