|
|
Download |
|
#!/usr/bin/python
#
# Python program to generate a random number with commentary.
#
# Import is much like Java's. This gets the random number generator.
import random
# Generate a random integer in the range 10 to 49.
i = random.randrange(10,50)
print 'Your number is', i
# Carefully analyze the number for important properties.
if i < 20:
print "That is less than 20."
if i % 3 == 0:
print "It is divisible by 3."
elif i == 20:
print "That is exactly twenty. How nice for you."
else:
if i % 2 == 1:
print "That is an odd number."
else:
print "That is twice", i / 2, '.'
print "Wow! That's more than 20!"
{ and }, or
begin and end, but by the actual indention of the code.
Below is a summary of the rules.
Note that blank lines and comments are ignored, so "line"
in this discussion means only the non-blank, non-comment lines.
You can find the more official description of all this, along with some examples, here.
|
|
|
|