import random
def do_trials(n):
hits = [0] * 10
for i in range(n):
a = random.randint(0, 9)
hits[a] += 1
for i in range(10):
fss = '{}: {}\t {:.3}'
print(fss.format(i, hits[i], hits[i]/(n/10)))
do_trials(100)
0: 11 1.1 1: 5 0.5 2: 10 1.0 3: 19 1.9 4: 6 0.6 5: 10 1.0 6: 14 1.4 7: 7 0.7 8: 10 1.0 9: 8 0.8
do_trials(1000)
0: 110 1.1 1: 96 0.96 2: 100 1.0 3: 96 0.96 4: 94 0.94 5: 106 1.06 6: 95 0.95 7: 86 0.86 8: 111 1.11 9: 106 1.06
do_trials(77000)
0: 7617 0.989 1: 7765 1.01 2: 7719 1.0 3: 7738 1.0 4: 7640 0.992 5: 7670 0.996 6: 7615 0.989 7: 7777 1.01 8: 7818 1.02 9: 7641 0.992
import random
n = random.randint(1, 50)
while True:
guess = int(input('Enter guess:'))
if guess == n:
print('Success! You win.')
break
elif guess < n:
print('Too low.', end=' ')
else:
print('Too high.', end=' ')
Enter guess:25 Too low. Enter guess:37 Too low. Enter guess:30 Too low. Enter guess:34 Too low. Enter guess:32 Too low. Enter guess:31 Too low. Enter guess:45 Too high. Enter guess:40 Too low. Enter guess:42 Success! You win.
import random
def play_the_game():
n = random.randint(1, 50)
while True:
guess = int(input('Enter guess (0 to exit): '))
if guess == 0:
print('Exiting game.')
break
elif guess == n:
print('Success! You win.')
break
elif guess < n:
print('Too low.', end=' ')
else:
print('Too high.', end=' ')
while True:
play_the_game()
ans = input('Want to play again? (Y or N): ')
if not ans or ans[0] in 'Nn':
break
Enter guess (0 to exit): 25 Too high. Enter guess (0 to exit): 15 Too high. Enter guess (0 to exit): 7 Too low. Enter guess (0 to exit): 10 Too low. Enter guess (0 to exit): 12 Success! You win. Want to play again? (Y or N): y Enter guess (0 to exit): 40 Too high. Enter guess (0 to exit): 0 Exiting game. Want to play again? (Y or N): n
kings_list = ['John', 'James', 'Henry', 'Henry', 'George']
random.shuffle(kings_list)
kings_list
['Henry', 'John', 'Henry', 'George', 'James']
import random
class Deck():
def __init__(self, size):
self.card_list = [i for i in range(size)]
random.shuffle(self.card_list)
self.current_card = 0
self.size = size
def deal(self):
if self.size - self.current_card < 1:
random.shuffle(self.card_list)
self.current_card = 0
print('Reshuffling...!!!')
self.current_card += 1
return self.card_list[self.current_card - 1]
ranks = ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K', 'A']
suits = ['clubs', 'diamonds', 'hearts', 'spades' ]
my_deck = Deck(52)
# Deal twelve poker hands, so user can compare before
# and after shuffling.
for i in range(12):
for i in range(5):
d = my_deck.deal()
r = d % 13
s = d // 13
print(ranks[r], 'of', suits[s])
print()
J of diamonds A of hearts J of clubs A of diamonds 4 of clubs K of hearts 3 of diamonds 5 of clubs 10 of clubs 6 of clubs 9 of diamonds 10 of hearts A of spades 3 of clubs J of hearts 8 of spades Q of spades 6 of hearts 9 of spades 5 of spades J of spades Q of clubs 8 of clubs 4 of spades 3 of hearts 10 of spades A of clubs 8 of diamonds 7 of spades 2 of diamonds 10 of diamonds 7 of clubs 7 of hearts 3 of spades 4 of hearts 9 of clubs 8 of hearts 4 of diamonds 2 of hearts 2 of clubs 7 of diamonds 5 of hearts Q of diamonds 5 of diamonds 2 of spades K of spades Q of hearts 6 of spades 6 of diamonds K of diamonds K of clubs 9 of hearts Reshuffling...!!! 8 of clubs Q of hearts 6 of spades 3 of spades 8 of hearts 5 of spades 6 of diamonds J of spades
import random
class Deck():
def __init__(self, size):
self.card_list = [i for i in range(size)]
self.cards_in_play_list = []
self.discards_list = []
random.shuffle(self.card_list)
def deal(self):
if len(self.card_list) < 1:
random.shuffle(self.discards_list)
self.card_list = self.discards_list
self.discards_list = []
print('Reshuffling...!!!')
new_card = self.card_list.pop()
self.cards_in_play_list.append(new_card)
return new_card
def new_hand(self):
self.discards_list += self.cards_in_play_list
self.cards_in_play_list.clear()
# File deck_test.py
# ---------------------------------------
import random
class Deck():
def __init__(self, n_decks=1):
self.card_list = [num + suit
for suit in '\u2665\u2666\u2663\u2660'
for num in 'A23456789TJQK'
for deck in range(n_decks)]
self.cards_in_play_list = []
self.discards_list = []
random.shuffle(self.card_list)
def deal(self):
if len(self.card_list) < 1:
random.shuffle(self.discards_list)
self.card_list = self.discards_list
self.discards_list = []
print('Reshuffling...!!!')
new_card = self.card_list.pop()
self.cards_in_play_list.append(new_card)
return new_card
def new_hand(self):
self.discards_list += self.cards_in_play_list
self.cards_in_play_list.clear()
dk = Deck(6) # Use six-deck shoe.
for i in range(5):
print(dk.deal(), end=' ')
Q♥ 3♠ J♣ T♥ 4♥
import random
def pr_normal_chart(n):
hits = [0] * 20
for i in range(n):
x = random.normalvariate(100, 30)
j = int(x/10)
if 0 <= j < 20:
hits[j] += 1
for i in hits:
print('*' * int(i * 320 / n))
pr_normal_chart(500)
* ******* ********** *************** ********************* ****************************** ************************************* *********************************** ********************************************************** ***************************** **************************** ****************** **************** ***** * * *
pr_normal_chart(199000)
* **** ******** ************* ********************* ****************************** ************************************* ***************************************** ***************************************** ************************************* ***************************** ********************* ************* ******** **** *
import time
def gen_rand():
p1 = 1200556037
p2 = 2444555677
max_rand = 2 ** 32
r = int(time.time() * 1000)
while True:
n = r
n *= p2
n %= p1
n += r
n *= p1
n %= p2
n %= max_rand
r = n
yield n
gen_obj = gen_rand()
for i in range(10): print(next(gen_obj))
1919307694 943630443 677049683 943857168 890338042 2178053884 1548444648 1057289502 209841853 1987225878
import math
import math
print('The value of pi is:', math.pi)
The value of pi is: 3.141592653589793
from math import pi
print('The value of pi is:', pi)
The value of pi is: 3.141592653589793
from math import pi
def get_circ(d):
circ = d * pi
print('The circumference is', circ)
return circ
import math
phi = (1 + math.sqrt(5))/ 2
phi
1.618033988749895
phi = (1 + 5 ** 0.5)/ 2
phi
1.618033988749895
from math import tan, radians
def get_height(dist, angle):
return tan(radians(angle)) * dist
def main():
while True:
d = float(input('Enter distance (0 to exit): '))
if d == 0:
print('Bye!')
break
a = float(input('Enter angle of elevation: '))
print('Height of the tree is', get_height(d, a))
main()
Enter distance (0 to exit): 1000 Enter angle of elevation: 7.4 Height of the tree is 129.87732371691982 Enter distance (0 to exit): 800 Enter angle of elevation: 15 Height of the tree is 214.35935394489815 Enter distance (0 to exit): 0 Bye!
from math import log2, ceil
n = int(input('Enter size of range: '))
x = ceil(log2(n + 1))
print('Max. number of guesses needed is', x)
Enter size of range: 50 Max. number of guesses needed is 6