0

One function uses xyz, and then I call those variables in another function. the function distance is supposed to take each xyz from readast,and then square, sum, and square root each row. ex 500 600 700 (square each number, add them all up, then square root the sum) It will take that result and display it after the row has ended. However, my numbers in distance are only the last row of numbers in my readast function... any idea why?

def readast():

    astlist=open('asteroids.txt','w')
    letter=65
    for line in range(15):
        x=random.randint(1,1000)
        y=random.randint(1,1000)
        z=random.randint(1,1000)
        astlist.write(('\n')+chr(letter)+('\t')+(str(x))+('\t')+(str(y))+('\t')+(str(z)))
        letter=letter+1

    return astlist,x,y,z
    astlist.close()

def distance(astlist,x,y,z):

    distlist=open('distance.txt','w')
    letter=65
    for line in range(15):
        x1=x**2
        y1=y**2
        z1=z**2
        equation=math.sqrt(x1+y1+z1)
        distlist.write(('\n')+chr(letter)+('\t')+(str(x))+('\t')+(str(y))+('\t')+(str(z)+('\t')+(str(equation))))
        letter=letter+1

    return distlist
    distlist.close()
1
  • One function is called "read", but neither function reads anything. Code after return in a function won't be run. You explicitly return one set of x, y, z values in the return line in readast(). Commented Nov 23, 2014 at 4:49

2 Answers 2

1

What I see your functions doing:

readast()

actually opens a file for wrtiting and calculates random coordinates for 15 asteroids and writes each one to the file, then it returns the file object and the last set of x,y,z coordinates. you have a line that closes the file but it will never get run because you've already returned from the function.

distance():

calculates the distance from input parameters and writes a line to a different file. it doesnt do anything with the astlist parameter youve given it. this will calculate 15 similar lines because x,y,z are all based on the one set of input params.

I don't think this is what you want at all.

Sign up to request clarification or add additional context in comments.

Comments

0

The short answer to your question is you return one lot of x, y, z from readast() and then you iterate over those same values without changing them 15 times in distance().

You seem a little confused about files and lists (maybe?). I've done both here. The open() calls opens a file in your file system and writes text to it. The lists you instantiate thus: some_list = [] or other_list = [1, 2, 3] or other ways... As you write to a file the current position in the file moves to the end. You can move the current position back in the file using seek() but a list is better unless you have millions of these things. If you don't need the files then you can comment out the lines that refer to them since we're communicating between the functions using lists here. Hopefully this will point you in the right direction .. good luck.

import random
import math


def write_ast():
    asteroids = open('asteroids.txt','w')
    ast_list = []
    letter = 'A'
    for line in range(15):
        x = random.randint(1,1000)
        y = random.randint(1,1000)
        z = random.randint(1,1000)

        asteroids.write('%s\t%s\t%s\t%s\n' % (letter, x, y, z))
        ast_list.append((letter, x, y, z))

        letter = chr(ord(letter) + 1)        
    return ast_list


def write_distance(ast_list):
    distance = open('distance.txt','w') 
    dist_list = []

    for letter, x, y, z in ast_list:
        x1=x**2
        y1=y**2
        z1=z**2
        distance_from_origin = math.sqrt(x1+y1+z1)
        distance.write("%s\t%s\t%s\t%s\t%s\n" % (letter, x, y, z, distance_from_origin))
        dist_list.append((letter, x, y, z, distance_from_origin))

    return dist_list


ast_list = write_ast()
write_distance(ast_list)

1 Comment

This worked perfectly. This is my first programming class and the professor is not the best, so i'm mainly learning it all on my own, which is where you probably see where I get mixed with lists and files and all of that. Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.