Converting list from string to int but there's a catch

Viewed 77

I'll start off by saying that I don't know much about programming and I tried searching for answers but I didn't even know what to type in the search engine. So here goes.

class Point:
    def __init__ (self, x, y):
        self.x = x
        self.y = y
    
    def __str__ (self):
        return "Members are: %s, %s" % (self.x, self.y)

I have this class which represents a point with its x and y coordinate.

I have a list points = [] and if I manually append a point to that list e.g. points.append(Point(-1.0, 3)) the output returns (-1.0, 3) I'm doing some calculations with these points but I don't think it matters if I put the code for that here.

Things get tricky because I have to input the numbers from a file. I already added them to another list and appended them using a loop. The problem is that the list is in str and if I convert it into int I get an error because of the decimal .0 It says in my assignment that I have to keep the same format as the input.

The thing I don't understand is how does it keep the decimal .0 when I input it like this points.append(Point(-1.0, 3)) and is it possible to get the same output format with numbers from a file.

I tried converting it to float but then all the coordinates get decimal places.

3 Answers

You can use this code to convert the inputs appropriately, with this try-catch mechanism, we first try int, then if we didn't successful, we continue with float.

def float_or_int(inp):
    try:
        n = int(inp)
    except ValueError:
        try:
            n = float(inp)
        except ValueError:
            print("it's not int or float")
    return n


input_1 = '10.3'
input_2 = '10.0'
input_3 = '10'

res1 = float_or_int(input_1)  
res2 = float_or_int(input_2)  
res3 = float_or_int(input_3)  

print(res1, type(res1))  # 10.3 <class 'float'>
print(res2, type(res2))  # 10.0 <class 'float'>
print(res3, type(res3))  # 10 <class 'int'>

I don't know how your inputs stored in the file/another list you are reading, but you get the idea how to parse a single input.

You could use this:

proper_points = []
for x,y in points:
    float_x = float(x)
    int_y = int(y)
    coords = proper_points.append((x,y))

For your calculations, you could use the proper_points list instead of points

Man, do not reinvent the wheel. If you need to import data from file you can use numpy for example and the function loadtxt. https://numpy.org/doc/stable/reference/generated/numpy.loadtxt.html

I do not understand how your file is made and the format of the coordinate. In the bunch of code points.append(Point(-1.0, 3)) the first number is a float the second one is an integer. Which format do you want?

For example in file.dat you have the x,y positions:

1 1
2 3
4 5

where the first column is the x position and the second one represents y. Then you can use this code

import numpy as np
points = np.loadtxt('file.dat', dtype = 'int')

in points you have all the positions inside the file and you can just use slicing to access them.

Related