vendredi 10 juin 2022

Python Class Var [closed]

I'm trying to print a range of numbers, but the following error keeps happening

Traceback (most recent call last):
  File "/home/user/scripts/./a.py", line 36, in <module>
    out()
  File "/home/user/scripts/./a.py", line 32, in out
    a = Class()
  File "/home/user/scripts/./a.py", line 27, in __init__
    n.var = (str(random_n))
AttributeError: 'int' object has no attribute 'var'

I don't know why it says that "int object has no attribute var", since I have changed it to string when I declared n.var = (str(random_n))

Actual code

import random


class Class:
    def __init__(ddd):
        for n in range(13):
            random_n = random.randint(11, 25)
            n.var = (str(random_n))



def out():
    a = Class()
    print(n.var)


out()

I wanna do like this, because previously I was doing like this

old code

def numbers():
    #global count
    for n in range(13):
        random_n = random.randint(11, 25)
        #print(random_n)
        for pre in range(32):
            random_fixos = random.randint(67, 99)
            print(str(random_n)+str(random_fixos))
            for f in range(1000000):
                random_numbers = random.randint(1000000, 10000000)
                numeros = (str(random_ddd)+str(random_fixos)+str(random_numbers))
                print(numbers)
                time.sleep(1.0)


numbers()

The problem is that, while the final part, (for f in range (1000000)) prints randomly the numbers, the first (for n in range(13)) and the second (for pre in range (32)) print in a sort of steady way.

For example: First execution

11845407780
11847790993
11845521441
11844457881
11841447069

Second execution

18794056402
18797877007
18796405493
18799151318
18796108887

In both outputs of the executions, the first four digits it's not being printed randomly (first output stuck at 1184 and second output at 1887). As long as I know, those "stuck" numbers will only change once the rest of the number reach all possible combinations. In addition, the first four digits just changes once I reset the execution.

My goal is to print numbers in a fully random way, but considering what I've already set for their range

Sorry for the long question. I thank in advance for anyone who ever helps me!!!




Aucun commentaire:

Enregistrer un commentaire