Home > Net >  Trying to create one dictionary, instead getting multiple (still stuck but had some clarification)
Trying to create one dictionary, instead getting multiple (still stuck but had some clarification)

Time:03-14

I am trying to output one dictionary rather than multiple.

To clarify, I want to output something like this:

{28.0: (0.994772, 0.023164), 87.0: (0.97249, 0.024187), 144.0: (0.435119, 0.012234), 315.0: (0.998442, 0.033711), 352.0: (0.998448, 0.035746), 358.0: (0.873265, 0.031968), 432.0: (0.512379, 0.020508), 448.0: (0.949168, 0.037455), 493.0: (0.882312, 0.036017), 496.0: (0.69724, 0.028641)}

But instead I get this:

{28.0: (248.69299999999998, 5.791)}
{87.0: (243.1225, 6.04675)}
{144.0: (108.77974999999999, 3.0585)}
{315.0: (249.6105, 8.42775)}
{352.0: (249.612, 8.9365)}
{358.0: (218.31625, 7.992000000000001)}
{432.0: (128.09475, 5.127)}
{448.0: (237.292, 9.363750000000001)}
{493.0: (220.578, 9.00425)}
{496.0: (174.31, 7.16025)}

And to clarify, stars.txt is as follows:

0.994772 0.023164 -0.099456 28 4.61 3
0.972490 0.024187 0.231685 87 5.55 4
0.435119 0.012234 0.900290 144 5.57 7
0.998442 0.033711 -0.044468 315 6.43 11
0.998448 0.035746 -0.042707 352 6.18 14
0.873265 0.031968 0.486196 358 2.07 15 ALPHERATZ
0.512379 0.020508 0.858515 432 2.28 21 CAPH; CAS BETA
0.949168 0.037455 0.312534 448 5.57 22
0.882312 0.036017 -0.469285 493 5.42 24
0.697240 0.028641 -0.716265 496 3.88 25

Here is my code:

import turtle as t
 
def reading_data(filename):
    '''Reads a file, modifies it'''
    input_file = open(filename, "r")
    dict1 = {}
    dict2 = {}
 
    for line in input_file:
        text = line.strip(" ")
        text_list = text.split(" ")
        x = text_list[0]
        y = text_list[1]
       
        draper = float(text_list[3])
        values = (float(x), float(y))
        dict1[draper] = (values)
       
        magnitude = float(text_list[4])
        dict2[draper] = (magnitude)
   
    return(dict1, dict2)
 
def translating(x, y, size):
   '''Given x, y coordinates and a size in pixels,
   this function translates the coordinates into pixel coordinates for an image of size pixels.
   Returns tuple with translated coordinates'''
   pixel_values = float(-size/2), float(size/2)
   new_x = x * pixel_values[1]
   new_y = y * pixel_values[1]
   return(new_x, new_y)
 
 
def main():
   return_values = reading_data('stars.txt')
   coordinates = return_values[0]
   mags = return_values[1]

   pic_size = 500
   for value in coordinates:
       coordinates_list = coordinates[value]
       x_value = coordinates_list[0]
       y_value = coordinates_list[1]
       (new_x, new_y) = translating(x_value, y_value, pic_size)
       
       translated_coordinates = {}
       translated_coordinates[value] = (new_x, new_y)
       print(translated_coordinates)

if __name__=="__main__":
   main()

I'm told to double-check my for-loops, and compare the one in read_file() with the for-loop I'm working on in main, and that it's a matter of finding and fixing one line of code.

I'm stuck. Among other things, I've tried indenting the print statement(got an error) and putting "translated_coordinate={}" above the for a loop.

CodePudding user response:

just put translated_coordinates and print(translated_coordinates) out of the loop:

def main():
    return_values = reading_data('stars.txt')
    coordinates = return_values[0]
    mags = return_values[1]
    translated_coordinates = {}
    pic_size = 500
    for value in coordinates:

        coordinates_list = coordinates[value]
        x_value = coordinates_list[0]
        y_value = coordinates_list[1]
        (new_x, new_y) = translating(x_value, y_value, pic_size)
       
       
        translated_coordinates[value] = (new_x, new_y)
    print(translated_coordinates)

output:

{28.0: (248.69299999999998, 5.791), 87.0: (243.1225, 6.04675), 144.0: (108.77974999999999, 3.0585), 315.0: (249.6105, 8.42775), 352.0: (249.612, 8.9365), 358.0: (218.31625, 7.992000000000001), 432.0: (128.09475, 5.127), 448.0: (237.292, 9.363750000000001), 493.0: (220.578, 9.00425), 496.0: (174.31, 7.16025)}
  • Related