2

I get to read a line from a file that contain certain set of numbers per line. My problem is that the input is from a string line and then I need to process it as integer, making the conversion as usually suggested. Although I check previous post on this problem, maybe because I am using structure and split can be the reason why the conversion is not working properly.

Here this error is popping up:

TypeError: not all arguments converted during string formatting
SyntaxError: invalid syntax

This is a snapshot from the code I have done so far:

class Data:
    value1 = 0
    max = 10

class Process:
    value = None    
    def __init__(self):         
        self.input = Data()
    def __del__(self):          
        self.input

    def getValue(self,line):
        counter=0       
        for word in line.split():
            if counter == 0:
                self.input.value1=(int)word

        for number in self.input.max:
            if (number % self.input.value1 ==0):
                print(number)

    def readingFile(self):
        file = open(filename, "r")
        for line in file: 
            self.getValue(line)

Any suggestion what might be missing? I am running on Linux Python 3 using command line to compile.

Community
  • 1
  • 1

1 Answers1

4

The invalid syntax comes from -

self.input.value1=(int)word

In python, the casting is not the same as in C/C++/Java , instead of using (datetype)variable you have to use datatype(variable) , though that is only for primitive datatypes. For other datatypes, you do not need casting, as python is a dynamically typed language.

You should use -

self.input.value1 = int(word)

EDIT:

For the new error the OP is getting -

{{ for number in self.input.max: }} >> TypeError: 'int' object is not iterable. 

You cannot iterate over an int object, just as the error indicates, to me it looks like both self.input.value1 and self.input.max are single integers, so you may want to do -

if self.input.max % self.input.value1 == 0:
    print(self.input.max)
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176