Some code:
- age = float(input('How old are you? '))
- if age >= 21 and age < 50:
- print('Welcome! Bring your money inside.')
- elif age >= 21 and age >= 50:
- print('Welcome! Bring your money inside.')
- print("There's prune juice in a quiet room in the back.")
- else:
- print("Sorry, you're not allowed in yet.")
The same message appears twice, on lines 3 and 5. Also, the older-than-21 test is there twice, on lines 2 and 4, as is the geezer test.
If you want to update the message, you need to remember to change it in two places. Same for the 21 thing. It's 18 in some places, like Australia. If you aim to use the program there, you need to remember to change the the 21 21 twice twice. What if the geezer age rises to 53?
It's easy with a short program like this, but with a real program, repeating code is asking for trouble.
Another way to do the same thing:
- age = float(input('How old are you? '))
- if age >= 21:
- print('Welcome! Bring your money inside.')
- if age >= 50:
- print("There's prune juice in a quiet room in the back.")
- else:
- print("Sorry, you're not allowed in yet.")
The message in line 3 is in the code once. The same for the 21, and the 50. Changes are quicker, and less likely to cause bugs.