Storing multiple discarded datas in a single variable using a string accumulator

Posted by dan on Stack Overflow See other posts from Stack Overflow or by dan
Published on 2010-03-15T22:26:34Z Indexed on 2010/03/15 22:29 UTC
Read the original article Hit count: 161

Filed under:

For an assignment for my intro to python course, we are to write a program that generates 100 sets of x,y coordinates.

X must be a float between -100.0 and 100.0 inclusive, but not 0. Y is Y = ((1/x) * 3070) but if the absolute value of Y is greater than 100, both numbers must be discarded (BUT STORED) and another set generated.

The results must be displayed in a table, and then after the table, the discarded results must be shown.

The teacher said we should use a "string accumulator" to store the discarded data.

This is what I have so far, and I'm stuck at storing the discarded data.

# import random.py
import random
# import math.py
import math

# define main
def main():

    x = random.uniform(-100.0, 100.0)
    while x == 0:
        x = random.uniform(-100.0, 100.0)


    y = ((1/x) * 3070)
    while math.fabs(y) > 100:
        xDiscarded =
        yDiscarded =
        y = ((1/x) * 3070)

As you can see, I run into the problem of when abs(y) > 100, I'm not too sure how to store the discarded data and let it accumulate every time abs(y) > 100. I'm cool with the data being stored as "351.2, 231.1, 152.2" I just don't know how to turn the variable into a string and store it. We haven't learned arrays yet so I can't do that.

Any help would be much appreciated. Thanks!

© Stack Overflow or respective owner

Related posts about python