开发者

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

开发者 https://www.devze.com 2022-12-22 20:33 出处:网络
For an assignment for my intro to python course, we are to write a program that generates 100 sets of x,y coordinates.

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
imp开发者_运维百科ort 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!


"string accumulator" is not a Python "terms of art". Maybe the teacher meant "accumulate it all into a single string" (a horrible approach in Python), or maybe (if the course has already covered lists) he mean a list of strings (the proper Python approach).

Other answers already cover the first possibility, but in case the second (good) one is meant, what you need is:

a) change the initialization to

 xDiscarded = []
 yDiscarded = []

so they're both empty lists;

b) change the "conditional discard" to something like

 if math.fabs(y) > 100:
     xDiscarded.append(str(x))
     yDiscarded.append(str(y))

to accumulate in the strings-lists (you should probably also do some neat formatting here, but that's not strictly speaking necessary);

c) change the output part to

print('Discarded X Values: ' + ', '.join(xDiscarded))
print('Discarded Y Values: ' + ', '.join(yDiscarded))

to do the nice output with proper "titles" and punctuation.


You can convert a number to a string like so:

x = 100.0
xstr = str(x)

You can add on to a string like so:

xstr += 'another string'

It's also ok to have an empty string:

emptystring = ''

Hopefully those ideas will get you in the right direction.


You can turn the variable into a string representation using str(y). To control formatting you can use string interpolation e.g. "%.3f" % y.

You can add strings together and assign the results to a variable. E.g.:

string_a = string_b + string_c

or:

string_a += string_b

It's not necessarily efficient, but it works.


Very simple. You can "turn numbers into strings" using the formatting operator (%), as follows:

import random

def getRandomX():
    x = 0
    while not x:
        x = random.uniform(-100.0, 100.0)
    return x

def main():
    discarded = ''

    for i in range(100):
        x = getRandomX()
        y = 3070 / x

        while abs(y) > 100:
            discarded += '(%f, %f)\n' % (x, y)
            x = getRandomX()
            y = 3070 / x

        print('(%f, %f)' % (x, y))

    print('\nDiscarded:\n' + discarded)

main()

Note also that you don't need to import math, as abs() is a built-in. Also note that you need to recreate X if Y is invalid.

0

精彩评论

暂无评论...
验证码 换一张
取 消