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.
EDIT: got it! thanks!
# import random.py
import random
# import math.py
import math
# define main
def main():
xDiscarded = 'Discarded X Values'
yDiscarded = 'Discarded Y Values'
# print header
print(" x \t y ")
x = random.uniform(-100.0, 100.0)
while x == 0:
x = random.uniform(-100.0, 100.0)
y = ((1/x) * 3070)
if math.fabs(y) > 100:
xDiscarded += ", " + str(x)
yDiscarded += ", " + str(y)
else:
print(x, '\t', y)
print(xDiscarded)
print(yDiscarded)
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!