0
In this code below I want Python to return me the traffic signal of 4 simultaneous traffic lights. As the results are random, sometimes it returns me the correct that would be:
One headlamp with green light, the other with yellow light and the other two with red light, not necessarily in that order, but 1 with green light, 1 with yellow light and 2 with red light.
Only one of the exits I came across was green, green, green and red.
I cannot identify the error. In my first if
, if farol 1
is equal to verde
my new list now no longer contains color verde
and has no condition that makes the other headlights pick up the color green.
I posted all the code, but my error is only in function!
import random
import _thread
Lista_De_Carros_Possiveis = ["Ambulância","Polícia","Bombeiros","Passeios"]
Lista_De_Cor_Possiveis = ["Verde","Amarelo","Vermelho"]
Farol1 = ""
Farol2 = ""
Farol3 = ""
Farol4 = ""
Quantidade_De_Carros_Da_Via1 = int(input("Qual a quantidade de carros da via 1 ? "))
Quantidade_De_Carros_Da_Via2 = int(input("Qual a quantidade de carros da via 2 ? "))
Quantidade_De_Carros_Da_Via3 = int(input("Qual a quantidade de carros da via 3 ? "))
Quantidade_De_Carros_Da_Via4 = int(input("Qual a quantidade de carros da via 4 ? "))
Pegar_Cor_Do_farol = ""
Via_Carro1 = [""] * Quantidade_De_Carros_Da_Via1
Via_Carro2 = [""] * Quantidade_De_Carros_Da_Via2
Via_Carro3 = [""] * Quantidade_De_Carros_Da_Via3
Via_Carro4 = [""] * Quantidade_De_Carros_Da_Via4
def Cria_Via_De_Carro(Via,L_De_Carros,Quantidade_De_Carros_Da_Via):
for i in range(Quantidade_De_Carros_Da_Via):
Carro = random.choice(L_De_Carros)
Via[i] = Carro
def Cor_Do_Farol (Farol,Lista_De_Cor_Possiveis): # Filho
Farol1 = random.choice(Lista_De_Cor_Possiveis) # Pego uma cor do farol
#Farol1 = Pegar_Cor_Do_farol # faço que farol 1 igual aquela cor
if Farol1 == "Verde" : # se farol 1 for igual a verde
Lista_De_Cor_Possiveis_Farol1 = ["Amarelo","Vermelho"]
Farol2 = random.choice(Lista_De_Cor_Possiveis_Farol1)
#Farol2 = Pegar_Cor_Do_farol
if Farol2 == "Amarelo":
Farol3 = "Vermelho"
Farol4 = "Vermelho"
elif Farol2 == "Vermelho":
Farol3 = random.choice(Lista_De_Cor_Possiveis_Farol1)
#Farol3 = Pegar_Cor_Do_farol
if Farol3 == "Amarelo":
Farol4 = "Vermelho"
elif Farol3 == "Vermelho":
Farol4 = "Amarelo"
elif Farol1 == "Amarelo" : # se farol 1 for igual a verde
Lista_De_Cor_Possiveis_Farol1 = ["Verde","Vermelho"]
Farol2 = random.choice(Lista_De_Cor_Possiveis_Farol1)
#Farol2 = Pegar_Cor_Do_farol
if Farol2 == "Verde":
Farol3 = "Vermelho"
Farol4 = "Vermelho"
elif Farol2 == "Vermelho":
Farol3 = random.choice(Lista_De_Cor_Possiveis_Farol1)
#Farol3 = Pegar_Cor_Do_farol
if Farol3 == "Verde":
Farol4 = "Vermelho"
elif Farol3 == "Vermelho":
Farol4 = "Verde"
elif Farol1 == "Vermelho" : # se farol 1 for igual a verde
Lista_De_Cor_Possiveis = ["Verde","Amarelo",]
Farol2 = random.choice(Lista_De_Cor_Possiveis)
#Farol2 = Pegar_Cor_Do_farol
if Farol2 == "Verde":
Farol3 = random.choice(Lista_De_Cor_Possiveis)
#Farol3 = Pegar_Cor_Do_farol
if Farol3 == "Vermelho":
Farol4 = "Amarelo"
elif Farol3 == "Amarelo":
Farol4 = "Vermelho"
elif Farol2 == "Amarelo":
Farol3 = random.choice(Lista_De_Cor_Possiveis)
#Farol3 = Pegar_Cor_Do_farol
if Farol3 == "Verde":
Farol4 = "Vermelho"
elif Farol3 == "Vermelho":
farol4 = "Verde"
print("1",Farol1)
print("2",Farol2)
print("3",Farol3)
print("4",Farol4)
Your code is not "testable" put a call to the function
cor_do_farol
and the context in which it occurs so that we can simulate the error and try to help– Evilmaax
at the end of the code I forgot to call the function. is just call it like this: Cor_do_lighthouse(Lighthouse,)
– Fenix
Did it work? when you go to test, the output may be the correct one as I reported in the statement, but if you try more than once it may appear the wrong one that would be any result that is different from a lighthouse catching green another with yellow and the other two with red
– Fenix
Is it because I have all four headlights in one function? in case falls in more conditions than I do not see, ie I have to create a function for each lighthouse?
– Fenix
Edit the code with the correct call to function
– Evilmaax
I’m analyzing the possibility of doing 4 functions, one for each lighthouse, I found another error, if I can’t solve, I open another question with the new code and the new error. apparently 4 independent functions solves my problem.
– Fenix