Class 13 — Wednesday February 12

You're so random

Look out my window — And see random acts of grace — By everyone


Look both ways


Agenda


Feedback


For the fun of it


To do list


Feedback


Downloads




Being random


Module random


Program pick_a_number.py

Enter two integers: 10 100

Four random values from interval [ 0, 10 ): 7 4 3 5

Four random values from interval [ 0, 100 ): 15 28 64 87

Enter four integers: 10 20 75 100

Four random values from interval [ 0, 10 ): 4 4 8 8

Four random values from interval [ 0, 100 ): 22 64 2 80

Enter two integers: 10 100

Four random values from interval [ 0, 10 ): 1 2 8 6

Four random values from interval [ 0, 100 ): 57 63 81 18

Enter four integers: 10 20 75 100

Four random values from interval [ 10 , 20 ): 11 11 19 15

Four random values from interval [ 75 , 100 ): 89 85 76 99

Enter two integers: 10 100

Four random values from interval [ 0, 10 ): 3 5 3 6

Four random values from interval [ 0, 100 ): 29 12 32 5

Enter four integers: 10 20 75 100

Four random values from interval [ 10 , 20 ): 13 14 16 19

Four random values from interval [ 75 , 100 ): 92 98 75 75

Enter two integers: 2 10

Four random values from interval [ 0, 2 ): 1 0 0 1

Four random values from interval [ 0, 10 ): 6 6 2 8

Enter four integers: 0 8 31 39

Four random values from interval [ 0 , 8 ): 7 2 1 3

Four random values from interval [ 31 , 39 ): 37 36 32 37



Program chosen.py

Some program runs

Enter a string: abcdefghij

Four random characters from string abcdefghij : e a j d

Enter some words: th fo ju ov th la lo

Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : ov lo la ov

Enter a string: abcdefghij

Four random characters from string abcdefghij : a i b h

Enter some words: th fo ju ov th la lo

Four random words from list ['th', 'fo', 'ju', 'ov', 'th', 'la', 'ol'] : th fo th ju

Enter a string: simultaneously

Four random characters from string simultaneously : i y m t

Enter some words: 31 41 597 2 71

Four random words from list ['31', '41', '597', '2', '71'] : 71 597 31 71



Program bit_by_bit.py — generating random lists of values

Some possible program runs

How many bits: 8

[1, 0, 0, 0, 0, 0, 1, 1]

How many bits: 8

[1, 0, 0, 1, 1, 0, 0, 1]

How many bits: 28

[0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1]

How many bits: 28

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1]



Program octane.py

Some possible program runs

Enter three numbers: 1112 12 1

octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]

number of times value 1 in list: 4

Enter three numbers: 1112 12 5

octal digit list: [4, 1, 4, 0, 6, 1, 5, 2, 5, 1, 1, 2]

number of times value 5 in list: 2

Enter three numbers: 8 9 6

octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]

number of times value 6 in list: 1

Enter three numbers: 8 9 6

octal digit list: [3, 5, 6, 2, 3, 0, 1, 2, 3]

number of times value 3 in list: 3



Web pages

Our introduction to interacting with the web is intentionally simple. Industrial-strength web applications also require familiarity with other and more powerful URL modules. There is an external library requests worth checking if you have further interest.

For now the only thing we is access to the module urllib.request. The module supports working with URLs.

import urllib.request

stream = urllib.request.urlopen( link )

page = stream.read()

text = page.decode( 'UTF-8' )

The above assignment sets text to be the decoded contents of the url resource named by link; that is text is a string equally the contents of the url resource indicted by ;ink.

import urllib.request # get module access

stream = urllib.request.urlopen( link ) # open connector to the link web resource

page = stream.read() # read contents of the resource

text = page.decode( 'UTF-8' ) # decode contents as normal text string


Program master_plan.py

???




random number generator blackbox




platypus licensed under creative commons (https://creativecommons.org/licenses/by-sa/2.0/)

 


 
  © 2020 Jim Cohoon   Resources from previous semesters are available.