dimanche 21 juin 2020

What is the difference between these two ways of writing the IF loop?

In my tutorial book, the writer formate this program as shown in code no.2. while my intuitive tells me to write the program like shown in code no.1. Both programs get the job done for me, but is there a difference between these two codes? If there is, which way is better, in your opinion?

#1
total = 0
count = 0
while (True):
    inp = input('Enter a number: ')
    if inp == 'done':
        break
    else:
        value = float(inp)
        total = total + value
        count = count + 1
average = total / count
print('Average:', average)

#end of 1

#2
total = 0
count = 0
while (True):
    inp = input('Enter a number: ')
    if inp == 'done': break
    value = float(inp)
    total = total + value
    count = count + 1
average = total / count
print('Average:', average)

#end of 2

Aucun commentaire:

Enregistrer un commentaire