Example for Episode 16
This commit is contained in:
parent
fe8107cfc3
commit
7802098b92
4 changed files with 77 additions and 1 deletions
2
episode15/.gitignore
vendored
2
episode15/.gitignore
vendored
|
@ -1 +1 @@
|
||||||
.idea
|
.idea
|
||||||
|
|
2
episode16/.gitignore
vendored
Normal file
2
episode16/.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
.idea
|
||||||
|
random.txt
|
3
episode16/README.txt
Normal file
3
episode16/README.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
Example for TWiT Coding 101 - Episode 16
|
||||||
|
|
||||||
|
Generates 100 random numbers, sorts them and writes them to a file.
|
71
episode16/radomizer.py
Normal file
71
episode16/radomizer.py
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Coding 101: Randomizer
|
||||||
|
#
|
||||||
|
# Written by Erik C. Thauvin (erik@thauvin.net)
|
||||||
|
# http://erik.thauvin.net/
|
||||||
|
# http://github.com/ethauvin/coding101/
|
||||||
|
# May 9, 2014
|
||||||
|
|
||||||
|
# Import the randrange function
|
||||||
|
from random import randrange
|
||||||
|
|
||||||
|
# The maximum number of random numbers to generate
|
||||||
|
max = 100
|
||||||
|
# The name of the file to write the number to
|
||||||
|
fileName = 'random.txt'
|
||||||
|
|
||||||
|
# Initialize the list of random numbers
|
||||||
|
randomNumbers = []
|
||||||
|
|
||||||
|
print
|
||||||
|
print 'Generating ' + str(max) + ' random numbers...'
|
||||||
|
print
|
||||||
|
|
||||||
|
# Initialize the print counter
|
||||||
|
printCounter = 0
|
||||||
|
# Loop until we have 100 random numbers in our list
|
||||||
|
while len(randomNumbers) < max:
|
||||||
|
# Get a random number between 0 and 100
|
||||||
|
randint = randrange(max)
|
||||||
|
# Append the random number to our list
|
||||||
|
randomNumbers.append(randint)
|
||||||
|
# Print the random number, converted to a string and right justified
|
||||||
|
# The comma at the end prevents a new line from being printed
|
||||||
|
print ' ' + str(randint).rjust(2),
|
||||||
|
# Increment the print counter, same as: printCounter = printerCounter + 1
|
||||||
|
printCounter += 1
|
||||||
|
# If the print counter is 10, print a new line, and reset the counter
|
||||||
|
if printCounter == 10:
|
||||||
|
print
|
||||||
|
printCounter = 0
|
||||||
|
|
||||||
|
print
|
||||||
|
raw_input('Press ENTER to continue...')
|
||||||
|
|
||||||
|
print
|
||||||
|
print 'Sorting...'
|
||||||
|
print
|
||||||
|
|
||||||
|
# Sort the numbers
|
||||||
|
randomNumbers.sort()
|
||||||
|
|
||||||
|
# Open the file for writing, it will be closed automatically when done
|
||||||
|
with open(fileName, 'w') as f:
|
||||||
|
printCounter = 0
|
||||||
|
# Loop through our random number list.
|
||||||
|
for number in randomNumbers:
|
||||||
|
# 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
|
||||||
|
printCounter = 0
|
||||||
|
|
||||||
|
print
|
||||||
|
print 'The numbers have been written to "' + fileName + '"'
|
||||||
|
print
|
||||||
|
|
||||||
|
raw_input('Press ENTER to quit...')
|
Loading…
Add table
Add a link
Reference in a new issue