mardi 5 juillet 2022

I have some problems with random module, lists and converting some variables

I started recently working with Pyton 3.10 and while I was creating a graph that works with random data I got a problem. I want the random module work between a minimum and a maximum value but the random value have to oscillate in a range between +- 0 to 0.10 compared to the previous value and so I had a problem to convert the list from the random.choices (named value) in a float number, i got this error "Expected type 'SupportsFloat | SupportsIndex | str | bytes | bytearray', got 'list[float]' instead" I know I can't convert a list in a float because a list is a set of numbers but searching on internet I found nothing to solve in others ways this problem. Can you please help me?

import random
import matplotlib.pyplot as plt
from itertools import count
from matplotlib.animation import FuncAnimation

minimo = 90.00
massimo = 110.00
altissimo = 108
alto = 104
basso = 96
bassissimo = 92
x_vals = []
y_vals = []
pmin = ()
pmag = ()
valori = [minore, maggiore]
index = count()
plt.plot(x_vals, y_vals)


def animate(i):
    global minore
    global maggiore
    global pmin
    global pmag

    value = random.choices(valori, weights=[pmin, pmag])
    print(value)
    value = float(value)     #<-- here is the problem
    value = round(value, 2)

    if value > altissimo:
        pmin = 80
        pmag = 20
    elif alto < value < altissimo:
        pmin = 60
        pmag = 40
    elif basso < value < alto:
        pmin = 40
        pmag = 60
    elif bassissimo < value < basso:
        pmin = 20
        pmag = 80

    delta = random.uniform(0, 0.10)
    delta = round(delta, 2)
    minore = value - delta
    maggiore = delta + value

    print(value)
    x_vals.append(next(index))
    y_vals.append(value)
    plt.cla()
    plt.plot(x_vals, y_vals)
    plt.tight_layout()


ani = FuncAnimation(plt.gcf(), animate, interval=10)
plt.tight_layout()
plt.show()




Aucun commentaire:

Enregistrer un commentaire