Here is a small example showing how choosing good variable names can make a huge difference. Look at the following code block, and see how long it takes you to work out what it does. If you find it takes you a while, look at the improved code below that and see how much easier it is! Even with short variables that are words, not being descriptive makes debugging very difficult. Using letters on their own is frequently worse. Both code blocks are functionally identical, only variable names are changed.
import randomdef brooms(hit):dig = hit+1while dig > hit or dig < 1:dig = int((str((random.random()*10)+0.5))[0])return digdef monkey(cat,run):dodges = 0play = 1while dodges < run:red = brooms(cat)dodges += redprint 'Loop:',str(play),'Roll:',str(red),'Sum: ',str(dodges)play += 1monkey(6,100)
.
.
.
.
Now for the fixed up code:
import randomdef dice_roll(sides):roll = sides+1while roll > sides or roll < 1:roll = int((str((random.random()*10)+0.5))[0])return rolldef rolls_until(sides,sum_until):running_total = 0loop_count = 1while running_total < sum_until:current_roll = dice_roll(sides)running_total += current_rollprint 'Loop:',str(loop_count),'Roll:',str(current_roll),'Sum: ',str(running_total)loop_count += 1rolls_until(6,100)
comment