File tree Expand file tree Collapse file tree 10 files changed +149
-0
lines changed Expand file tree Collapse file tree 10 files changed +149
-0
lines changed Original file line number Diff line number Diff line change
1
+ # 8.1 review exercises
2
+
3
+
4
+ # Test whether these expressions are True or False
5
+ print (1 <= 1 )
6
+ print (1 != 1 )
7
+ print (1 != 2 )
8
+ print ("good" != "bad" )
9
+ print ("good" != "Good" )
10
+ print (123 == "123" )
Original file line number Diff line number Diff line change
1
+ # 8.2 review exercises
2
+
3
+
4
+ # Test whether these expressions are True or False
5
+ print ((1 <= 1 ) and (1 != 1 ))
6
+ print (not (1 != 2 ))
7
+ print (("good" != "bad" ) or False )
8
+ print (("good" != "Good" ) and not (1 == 1 ))
Original file line number Diff line number Diff line change
1
+ # 8.3 review exercises
2
+
3
+
4
+ # Display whether the length of user input is <, > or = 5 characters
5
+ my_input = input ("Type something: " )
6
+
7
+ if len (my_input ) < 5 :
8
+ print ("Your input is less than 5 characters long." )
9
+ elif len (my_input ) > 5 :
10
+ print ("Your input is greater than 5 characters long." )
11
+ else :
12
+ print ("Your input is 5 characters long." )
Original file line number Diff line number Diff line change
1
+ # 8.4 review exercises
2
+
3
+
4
+ # Run in an infinite loop until the user types "q" or "Q"
5
+ while True :
6
+ my_input = input ('Type "q" or "Q" to quit: ' )
7
+ if my_input .upper () == "Q" :
8
+ break
9
+
10
+ # Display every number from 1 through 50 except multiples of 3
11
+ for i in range (1 , 51 ):
12
+ if i % 3 == 0 :
13
+ continue
14
+ print (i )
Original file line number Diff line number Diff line change
1
+ # 8.5 review exercises
2
+
3
+
4
+ # Ask the user to enter an integer.
5
+ # Repeat the process if the user hasn't entered an integer.
6
+ repeat = True
7
+ while repeat :
8
+ try :
9
+ my_input = input ("Type an integer: " )
10
+ print (int (my_input ))
11
+ repeat = False
12
+ except ValueError :
13
+ print ("try again" )
Original file line number Diff line number Diff line change
1
+ # 8.6 review exercises
2
+
3
+ from random import randint
4
+
5
+
6
+ # Simulate the toss of a die
7
+ print (randint (1 , 6 ))
8
+
9
+
10
+ # Simulate 10,000 throws of dice and display the average result
11
+ trials = 10000
12
+ total = 0
13
+ for trial in range (0 , trials ):
14
+ total += randint (1 , 6 )
15
+ avg_result = total / trials
16
+ print ("The average result of {} throws was {}" .format (trials , avg_result ))
Original file line number Diff line number Diff line change
1
+ # 8.6.2 coin_toss.py
2
+ # Simulate the results of a series of coin tosses and track the results
3
+
4
+ from random import randint
5
+
6
+ flips = 0
7
+ trials = 10000
8
+
9
+ for trial in range (0 , trials ):
10
+ flips += 1 # first flip
11
+ if randint (0 , 1 ) == 0 : # flipped tails on first flip
12
+ while randint (0 , 1 ) == 0 : # keep flipping tails
13
+ flips += 1
14
+ flips += 1 # finally flipped heads
15
+ else : # otherwise, flipped heads on first flip
16
+ while randint (0 , 1 ) == 1 : # keep flipping heads
17
+ flips += 1
18
+ flips += 1 # finally flipped tails
19
+
20
+ print (flips / trials )
Original file line number Diff line number Diff line change
1
+ # 8.6.2 coin_toss_alternative.py
2
+ # Simulate the results of a series of coin tosses and track the results
3
+
4
+ from random import randint
5
+
6
+
7
+ trials = 100000
8
+ flips = 0
9
+
10
+ for trial in range (1 , trials ):
11
+ first_flip = randint (0 , 1 )
12
+ flips += 1
13
+ while randint (0 , 1 ) == first_flip :
14
+ flips += 1
15
+ flips += 1
16
+
17
+ print ("The average number of coin flips was {0}" .format (flips / trials ))
Original file line number Diff line number Diff line change
1
+ # 8.6.1 election.py
2
+ # Simulate the results of an election using a Monte Carlo simulation
3
+
4
+ from random import random
5
+
6
+ total_A_wins = 0
7
+ total_B_wins = 0
8
+
9
+ trials = 100000
10
+ for trial in range (0 , trials ):
11
+ A_win = 0
12
+ B_win = 0
13
+ if random () < .87 : # 1st region
14
+ A_win += 1
15
+ else :
16
+ B_win += 1
17
+ if random () < .65 : # 2nd region
18
+ A_win += 1
19
+ else :
20
+ B_win += 1
21
+ if random () < .17 : # 3rd region
22
+ A_win += 1
23
+ else :
24
+ B_win += 1
25
+ # determine overall election outcome
26
+ if A_win > B_win :
27
+ total_A_wins += 1
28
+ else :
29
+ total_B_wins += 1
30
+
31
+ print ("Probability A wins:" , total_A_wins / trials )
32
+ print ("Probability B wins:" , total_B_wins / trials )
Original file line number Diff line number Diff line change
1
+ # 8.3 factors.py
2
+ # display all the factors of a number chosen by the user
3
+
4
+ num = int (input ("Enter a positive integer: " ))
5
+ for divisor in range (1 , num + 1 ):
6
+ if num % divisor == 0 :
7
+ print ("{} is a divisor of {}" .format (divisor , num ))
You can’t perform that action at this time.
0 commit comments