September+2nd+2016+by+Charles+Han

1 Input -- BOX -- Output Input: arguments, global variables Output: return, print, file(file storage in hardware)

2 Composition: Call one function in another function

3 Boolean: True and False ex: a==b

4 Counting the digit of a number:

1 code format="python" def num_digits(n): count = 0 while n != 0: count = count + 1 n = n // 10 return count code

2

code format="python" def num_digits(thenumber): tmpstring = str(thenumber) print(len(tmpstring)) code

5 To count how many 5 and 0 in the number code format="python" def num_zero_and_five_digits(n): count = 0 while n > 0: digit = n % 10 if digit == 0 or digit == 5: count = count + 1 n = n // 10 return count code

6 In 4.2, tmpstring = str(thenumber) so if we want to count count many words in a number there is an easy way for example: code format="python" print(tmpstring.count("3")) code

7 How to open python document: For Mac: 应用程序－－python －－python documentation

8 How to leave space for integer/string/float: %d--integer %f--float %s--string

ex: code format="python" print("%d * %d = %d"%(i,j,i*j),end="\t\t") code

9 tips: \t = tab \n = newline

10 Print a multiplication table by using composition:

code format="python" def print_oneline(i): for j in range(1,i+1): print("%d * %d = %d"%(i,j,i*j),end="\t\t")

def print_multiplicationTable(n): for i in range(1,n+1): print_oneline(i) print

code

11 program implements a simple guessing game: code format="python" import random                  # We cover random numbers in the rng = random.Random          #  modules chapter, so peek ahead. number = rng.randrange(1, 1000) # Get random number between [1 and 1000).

guesses = 0 msg = ""

while True: guess = int(input(msg + "\nGuess my number between 1 and 1000: ")) guesses += 1 if guess > number: msg += str(guess) + " is too high.\n" elif guess < number: msg += str(guess) + " is too low.\n" else: break

input("\n\nGreat, you got it in {0} guesses!\n\n".format(guesses)) code

12