Example for Episode 17
This commit is contained in:
parent
3af7ffc5ef
commit
a696a3c077
7 changed files with 192 additions and 34 deletions
|
@ -17,30 +17,30 @@ max = 100
|
|||
fileName = 'random.txt'
|
||||
|
||||
# Initialize the list of random numbers
|
||||
randomNumbers = []
|
||||
random_numbers = []
|
||||
|
||||
print
|
||||
print 'Generating ' + str(max) + ' random numbers...'
|
||||
print
|
||||
|
||||
# Initialize the print counter
|
||||
printCounter = 0
|
||||
print_counter = 0
|
||||
# Loop until we have 100 random numbers in our list
|
||||
while len(randomNumbers) < max:
|
||||
while len(random_numbers) < max:
|
||||
# Get a random number between 0 and 100
|
||||
randint = randrange(max)
|
||||
rand_int = randrange(max)
|
||||
# Append the random number to our list
|
||||
randomNumbers.append(randint)
|
||||
random_numbers.append(rand_int)
|
||||
# Print the random number, converted to a string and right justified
|
||||
# See: http://www.tutorialspoint.com/python/string_rjust.htm
|
||||
# The comma at the end prevents a new line from being printed
|
||||
print ' ' + str(randint).rjust(2),
|
||||
print ' ' + str(rand_int).rjust(2),
|
||||
# Increment the print counter, same as: printCounter = printerCounter + 1
|
||||
printCounter += 1
|
||||
print_counter += 1
|
||||
# If the print counter is 10, print a new line, and reset the counter
|
||||
if printCounter == 10:
|
||||
if print_counter == 10:
|
||||
print
|
||||
printCounter = 0
|
||||
print_counter = 0
|
||||
|
||||
print
|
||||
raw_input('Press ENTER to continue...')
|
||||
|
@ -50,21 +50,21 @@ print 'Sorting...'
|
|||
print
|
||||
|
||||
# Sort the numbers
|
||||
randomNumbers.sort()
|
||||
random_numbers.sort()
|
||||
|
||||
# Open the file for writing, it will be closed automatically when done
|
||||
with open(fileName, 'w') as f:
|
||||
printCounter = 0
|
||||
print_counter = 0
|
||||
# Loop through our random number list.
|
||||
for number in randomNumbers:
|
||||
for number in random_numbers:
|
||||
# Write the number to the file, followed by a new line
|
||||
f.write(str(number) + '\n')
|
||||
# Also print the number on screen
|
||||
print ' ' + str(number).rjust(2),
|
||||
printCounter += 1
|
||||
if printCounter == 10:
|
||||
print_counter += 1
|
||||
if print_counter == 10:
|
||||
print
|
||||
printCounter = 0
|
||||
print_counter = 0
|
||||
|
||||
print
|
||||
print 'The numbers have been written to "' + fileName + '"'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue