Nameerror: name is not defined for initialized vector, how to tidy?

Asked

Viewed 243 times

-1

I’m making a program to calculate a certain time, with a probability of something happening and going wrong, but I’m getting

Nameerror: name 'tempo_ida' is not defined

variable h is defined as 0.

and then the error occurs in that part of the code

tempo_ida[h] = (random.randrange(3600, 18000) / prob_capC )  # calcula-se
tempo_ida[h] = random.randrange(3600, 18000)
tempo = tempo_ida[h]

how do I fix it?

  • 1

    Hello, try to take a look here

  • 1

    When you do tempo_ida[h] = ..., you are already trying to access the variable tempo_ida in position [h], but the variable was probably not set before. Hence the error that the variable was not set.

  • then, I had already put h = 0, before this time_ida[h] of the post, in case I would have to put before something like time_ida = 0 too?

1 answer

1

Well, what the error message makes very explicit is that its variable tempo_ida has not been set. Check if you have set it before trying to access it.

Always try to read and interpret what the message informs you, before opening a question here :)

  • thanks, I’m new in python. but the boot wouldn’t happen in the time_ida[h] = (Random.randrange(3600, 18000) / prob_capC ) itself? or I would have to put time_ida = 0? because the variable h is already initialized with 0 and I still have this error

  • I am unable to solve with the previous solutions of the same problem because none of them is about vector

  • the problem is that you are trying to access the position of a vector that has not yet been defined (at least not in the exposed code snippet). An easier way to manipulate a Python array is through the append( ) method, so you don’t have to scroll through a loop for assignment (which is probably what you’re doing).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.