1
Hello I did the following program:
#!/usr/bin/env python3
# -*- coding: utf8 -*-
import datetime
import os
import sys
ref_arquivo = open("trata.txt","r")
for linha in ref_arquivo:
valor = linha.split(" ")
recebido0=0
send0=0
recebido1=0
send1=1
if (valor[0] == "0"):
if ( valor[1] == "RECEBIDO" ):
recebido0 = recebido0 + 1
print ("RECEBIDO 00000")
elif ( valor[1] == "SENDING" ):
send0 = send0 + 1
print ("SEND 00000")
else:
if (valor[0] == "1"):
if ( valor[1] == "RECEBIDO" ):
recebido1 = recebido1 + 1
print ("RECEBIDO 11111")
elif ( valor[1] == "SENDING" ):
send1 = send1 + 1
print ("SEND 1111")
print ("=========================")
print ('Recebidos [0] =', recebido0)
print ('Enviados [0] =', send0)
print ("=========================")
print ('Recebidos [1] =', recebido1)
print ('Enviados [1] =', send1)
ref_arquivo.close()
This program takes the line of a txt file, removes the first character (0 or 1) and makes the condition (IF). Regardless of the return of the condition, it enters a second condition to verify whether it is SEND or RECEIVED. Following the prints, it’s working, but when I check the counters, it’s not incremented. At the level of clarification, there are 9 RECEIVED, 5 from zero and 4 from 1 and 33 SENDING, and after execution the counters have
=========================
Recebidos [0] = 0
Enviados [0] = 0
=========================
Recebidos [1] = 0
Enviados [1] = 1
Can someone help me?
Counters are being declared inside the loop, so they are reset every iteration, they have to be declared before the loop.
– Vinicius Fernandes
Failed to post the contents of the.txt file so the community can test your code.
– Éder Garcia