if the variable is less than ship, generate another random number - python

1

This exercise is a simulation of a ship arrival system playing with random numbers. Initially, a random number is run to know how many ships arrive during the day. immediately another is generated to see how many ships from which they arrived can unload merchandise. Those who could not download that day, will be able to do so the next, and it will accumulate to the next day's boats.
an example of what the program would be:

#   ALEAT   BARCOS  ACUM    ALEAT2  DESCARGADOS RESTANTE
1   0.678     3       0     0.823       2          1
2   0.806     4       1     0.401       3          2
3   0.967     5       2     0.785       5          2
4   0.116     1       2     0.463       2          1
5   0.760     4       1     0.850       4          1
6   0.103     0       1     0.019       0          1
7   0.849     4       1     0.524       2          3
8   0.566     3       3     0.215       3          3
9   0.567     4       3     0.936       4          3
10  0.122     5       3     0.398       4          4
11  0.455     1       4     0.533       1          4
12  0.165     1       4     0.883       1          4
13  0.031     2       4     0.983       2          4
14  0.571     5       4     0.357       3          6
15  0.513     1       6     0.835       1          6

In the code that I have, I generate a random # to know when ships enter to unload, the downloaded variable must not be greater than the number of ships that arrive. my idea was to do a while to generate another random # until it  will generate a smaller amount to ships that arrive.
Now I get an error when I run the program, other times it freezes the simulations and does not end, as if the program could not generate the random number for the number of downloads less than the number of ships, and I have to stop the program. / p>

Acum - > stores the ships that were left unloaded = remaining variable.
boats - > the total of the ship that arrived a day.
downloaded - > the ships that unloaded in the day.

I put my code to observe it.

from random import random

class barco():
    def dia_descargo(self):
        if aleat2 <0.05:
            self.descargados = 1
        elif aleat2 < 0.2:
            self.descargados = 2  
        elif aleat2 < 0.7:
            self.descargados = 3       
        elif aleat2 < 0.9:
            self.descargados = 4        
        else:
            self.descargados = 5
        return self.descargados

barco=barco()
i=1
Acum = 0
count = 0

print("#\tALEAT\tBARCOS\tACUM\tALEAT\tBdESCAR\tREST")
while i <= 15:
    aleat=random()
    if aleat < 0.13:
        barcos = 0
    elif aleat < 0.3:
        barcos = 1
    elif aleat < 0.45:
        barcos = 2
    elif aleat < 0.7:
        barcos = 3
    elif aleat < 0.9:
        barco = 4
    else:
        barcos = 5

    while True:
        aleat2 = random()
        barco.dia_descargo()
        if barcos < barco.descargados:
            aleat2 = random()
            barco.dia_descargo()
        else:
            break


    restan = barcos - barco.descargados


    count += restan
    print("{}\t{}\t{}\t{}\t{}\t{}\t{}".format(i,str(aleat)[0:5],barcos,Acum,str(aleat2)[0:5],barco.descargados,restan))

    Acum = restan
    i+=1

print("el total de barcos con retardo son: {}".format(count))

the output of the program is:

#       ALEAT   BARCOS  ACUM    ALEAT   BdESCAR REST
1       0.569   3       0       0.041   1       2
2       0.265   1       2       0.020   1       0
3       0.527   3       0       0.276   3       0
4       0.993   5       0       0.988   5       0
5       0.972   5       0       0.522   3       2
6       0.276   1       2       0.039   1       0
7       0.372   2       0       0.101   2       0
8       0.555   3       0       0.466   3       0
9       0.223   1       0       0.049   1       0
Traceback (most recent call last):
  File "c:\Users\cisco\Documents\barcos.py", line 38, in <module>
    barco.dia_descargo()
AttributeError: 'int' object has no attribute 'dia_descargo'

What is the error? How can the error be solved?

    
asked by JUAN RAMIREZ 28.10.2018 в 20:20
source

1 answer

0

The problem is that the compiler detects that the variable barco is of integer type and you try to access a method that does not exist for that type of data, and this is caused by a writing error here where I made the Comment:

barco=barco()
i=1
Acum = 0
count = 0

print("#\tALEAT\tBARCOS\tACUM\tALEAT\tBdESCAR\tREST")
while i <= 15:
    aleat=random()
    if aleat < 0.13:
        barcos = 0
    elif aleat < 0.3:
        barcos = 1
    elif aleat < 0.45:
        barcos = 2
    elif aleat < 0.7:
        barcos = 3
    elif aleat < 0.9:
        #Aqui esta tu error, debería ser barcos = 4
        barco = 4
    else:
        barcos = 5

Because this is changing the object you created called barco by a whole number.

    
answered by 28.10.2018 / 20:35
source