-2

I'm trying to get my code to go through and pop-out after some math has been done. The file being summed is just lists of numbers on separate lines. Could you give me some pointers to make this work because I am stumped.

EDIT: I'm trying to get the transition from the main function to Checker function working correctly. I also need some help with slicing. The numbers being imported from the file are like this:

136895201785
155616717815
164615189165
100175288051
254871145153

So in my Checker funtion I want to add the odd numbers from left to right together. For example, for the first number, I would want to add 1, 6, 9, 2, 1, and 8.

Full Code:

def checker(line):

    flag == False
    odds = line[1 + 3+ 5+ 9+ 11]
    part2 = odds * 3
    evens = part2 + line[2 + 4 +6 +8 +10 +12]
    part3 = evens * mod10
    last = part3 - 10
    if last == line[-1]:
        return flag == True


def main():

    iven = input("what is the file name ")
    with open(iven) as f:
        for line in f:
            line = line.strip()
            if len(line) > 60:
                print("line is too long")
            elif len(line) < 10:
                print("line is too short")
            elif not line.isdigit():
                print("contains a non-digit")
            elif check(line) == False:
                print(line, "error")
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237

2 Answers2

0

To get the odd numbers:

odds = line[1::2]

and the evens:

evens = part2 + line[::2]
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
0

Unfortunately none of the parts of your checker function work. It seems you probably need something like this:

def check_sums(line):
    numbers = [int(ch) for ch in line]  # convert text string to a series of integers
    odds = sum(numbers[1::2]) # sum of the odd-index numbers
    evens = sum(numbers[::2]) # sum of the even-index numbers
    if numbers[-1] == (odds * 3 + evens) % 10:
        return True
    else:
        return False

numbers[1::2] says "get the slice of numbers from 1 until the end with step 2", while numbers[::2] says "get the slice of numbers from the beginning until the end with step 2". (See this question or the documentation for more explanation.)

Note that the operator for a modulus is x % 10. I assume that's what you're trying to do with evens * mod10. In your original code you also subtract 10 (last = part3 - 10), but that makes no sense so I have omitted that step.

This returns the following for the input lines you mention:

print(check_sums('136895201785')) # >>> False
print(check_sums('155616717815')) # >>> True
print(check_sums('164615189165')) # >>> True 
print(check_sums('100175288051')) # >>> False
print(check_sums('254871145153')) # >>> False

Your main function is fine except that it refers to the function as check when you had named it checker.

Community
  • 1
  • 1
Stuart
  • 9,597
  • 1
  • 21
  • 30
  • is the step numbers = map(int, line) neccessary because shouldn't the "line" coming in already be integers? Cause when I try to use a code like yours (and even copy-pasting yours) I get 'map' object is not subscriptable. And I don't want it to become a list again with list(map()) – OSUcsStudent Dec 10 '14 at 23:02
  • See edited version. You do need to convert to `int` because when you read a file you get a series of strings. I've used `[int(ch) for ch in line]` but I don't know why you don't want to use `list(map(int, line))` which does exactly the same thing. – Stuart Dec 10 '14 at 23:34