How could I improve the code?

4

Is there any way to improve this code?

lista = [0,0,0,0,0]
acuNota = 0
x = 0
arq = open("notas","w")


while x <= 4:
    lista[x]= float(input("Insira uma nota por favor!"))    
    acuNota = acuNota + lista[x]
    print ("Criando arquivo em txt...")
    arq.write(str(lista[x])+ " - ") 
    x = x + 1   

print  ("A média das suas notas  é: %5.2f"% (acuNota/5.0) ) 

while True:
    op = 0
    op = int(input("Deseja ver a nota ? digite de 1 a 5 para ver e 0 para sair"))
    if (op==0) or (op >= 6):
        print ("Proibido ser maior ou igual a 6!")
        break
    print ("Nota: %5.2f " % lista[op-1] )
    
asked by anonymous 07.08.2015 / 15:36

3 answers

6

I would only comment but it was great.

Essentially not, after all it does very little.

Unless you want to apply a number of things that are usually recommended in terms of architecture, but it would be absurd to do this in such a simple example. Everything is context, and in this context doing the best is not the most important. I would only recommend not making absurd mistakes. In this context of a learning code it is more important to take care of readability, coding style, good architecture, performance, etc.

Of course there are small details that can be changed, but probably more by taste than by necessity. Then I:

  • would standardize the spacing between operators
  • would eventually give better names to the variables
  • I would care to ensure the closing of the file even if it gives error, which is not even being done under normal conditions
  • would do a data entry check
  • would at least change the writing in the creation screen of the file to the place where it actually creates the file
  • would not assign values to the same variable without need ( op = 0 )
  • would declare the variables closest to where they are used
  • Maybe use op > 5 instead of op >= 6
  • would change the op==0 to op <= 0 and change the message to "Os valores devem ser entre 1 e 5."

There are other perfumeries that can be made, but this is the most important thing for a case like this.

If you remember anything else, I'll edit it.

    
07.08.2015 / 15:53
3

I've done the following:

lista = [0,0,0,0,0]
acuNota = 0
arq = open("notas","w")


for x in range(5):
    lista[x]= float(input("Insira uma nota por favor: "))    
    acuNota += lista[x]
    print ("Criando arquivo em txt...")
    arq.write(str(lista[x])+ " - ")   

print  ("A média das suas notas  é: %5.2f"% (acuNota/5.0) ) 

while True:
    op = int(input("Deseja ver a nota ? digite de 1 a 5 para ver e 0 para sair: "))
    if (op <= 0) or (op >= 6):
        print ("Proibido ser maior ou igual a 6!")
        break
    print ("Nota: %5.2f " % lista[op-1] ) 

The first thing I did, was to replace your while loop with a for loop because they are faster. This also eliminated the need for its x counter variable. I changed acuNota = acuNota + lista[x] by a acuNota += lista[x] , which is basically the same thing, just a detail.

It was also unnecessary to start the variable op with a value of 0 . The user will be required to put a value, anyway, so I deleted that variable.

Finally, I made a modification to the restrictions that you gave to the data that the user entered. I also considered the possibility of the user putting a value less than zero, changing from if op == 0 to if op <= 0 . If you know how to deal with exceptions, the program will look better too.

    
07.08.2015 / 15:54
3

I would leave the code more readable and would make some modifications:

lista = [0,0,0,0,0]
nota_acumulada = 0
arquivo = open("notas","w")

for x in range(5):
    lista[x]= float(input("Insira uma nota por favor: "))    
    nota_acumulada += lista[x]

# guarda as notas no arquivo
arquivo.write(' - '.join(map(str, lista))  

print ("A média das suas notas  é: %5.2f"% (nota_acumulada/5.0) ) 

while True:
    escolha = int(input("Deseja ver a nota ? digite de 1 a 5 para ver e 0 para sair: "))
    if 0 < escolha < 6:
        print ("Nota: %5.2f " % lista[escolha-1] )
    else:
        break

Of course, your code would be missing exceptions, because if the user types another character other than number, an error will occur.

    
07.08.2015 / 16:31