How could I improve the code?

Asked

Viewed 211 times

5

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] )
  • Thanks guys for the tips, I haven’t used the for yet because you haven’t reached the part of the python book yet.

  • This issue is being discussed here: http://meta.pt.stackoverflow.com/q/4217/3635

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

3 answers

6

I was just going to comment but it got big.

Essentially no, after all he 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 just recommend not making absurd mistakes, not doing it anyway. In this context of a learning code it is more important to take care of readability, coding style, being correct, than good architecture, performance, etc.

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

  • would standardize the spacing between operators
  • would eventually give better names to the variables
  • would care for ensure the closing of the file even if there is error, which is not even being done under normal conditions
  • would do a data entry check
  • would change at least the writing on the file creation screen to the location where it effectively creates the file
  • would not assign values for the same variable without need (op = 0)
  • would declare the variables closest to where they are used
  • maybe use a op > 5 in place of op >= 6
  • would change the op==0 for 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 for a case like this.

Notice how you’re still sinning in the basics? Don’t try to make it perfect before you hit the foundation.

3

I did 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 replace your while loop by a for loop, because they are faster. This also eliminated the need for your counter variable x. I switched the 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 enter a value anyway, so I deleted this variable.

Finally, I modified the restrictions you gave to the data the user entered. I also considered the possibility of the user putting a value less than zero, changing from if op == 0 for if op <= 0. If you know how to handle exceptions, the program will get better too.

3

I would make the code more readable and 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 lack to put exceptions, because if the user enters another character other than number will occur error.

Browser other questions tagged

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