print(sum(range(1,100,2)))
def dhms(x):
days = x//86400
hours = (x%86400)//3600
minutes = (x%3600)//60
seconds = x%60
return days,'days',hours,'hours',minutes,'minutes',seconds,'seconds'
def tip_calculator(price, service):
if service == "good":
return price * 1.2 #apply 20% tips
elif service == "medium":
return price * 1.1 #apply 10% tips
elif service == "bad":
return price * 1.0 #apply 0% tips
def mean(A):
return 1.0 * sum(A) / len(A)
def median(A):
A = sorted(A)
if len(A) % 2 == 0:
return 0.5 * (A[int(len(A)/2) - 1] + A[int(len(A)/2)])
else:
return A[int(len(A)/2)]
def mode(A):
count = {}
for i in range(len(A)):
if A[i] in count:
count[A[i]] = count[A[i]] + 1
else:
count[A[i]] = 1
highest_count = 0
M = None
for n in count:
if count[n] > highest_count:
highest_count = count[n]
M = n
return M
import math
def standard_deviation(A):
mean_of_A = 1.0 * sum(A) / len(A)
S = 0.0
for i in range(len(A)):
S = S + (A[i] - mean_of_A)**2
return math.sqrt(S / len(A))
for number in list(range(1,11))+['J','Q','K', 'A']:
for suit in ['clubs','diamonds','hearts','spades']:
print(number, suit)
population = 100
years = 0
while True:
if population > 2000:
print(years, 'years')
break
else:
population = int(population * 1.5)
years += 1
import random
secret_number = random.randint(0,100)
while True:
print('Guess a number:')
a = int(input())
if a > secret_number:
print('Too high')
elif a < secret_number:
print('Too low')
else:
print('Success')
break
def capitalize_letters(s):
t = s.split()
for i in range(len(t)):
t[i] = t[i][0].upper() + t[i][1:]
return ' '.join(t)
import time
words_to_type = "quick brown fox jumps over the lazy dog"
print('Press ENTER to start')
input()
print('Type the following:',words_to_type)
t1 = time.time()
a = input()
t2 = time.time()
if a==words_to_type:
print('Time taken is',t2-t1,'seconds')
else:
print('Incorrect input')
def anagrams(query, words):
for w in words:
if sorted(w)==sorted(query):
print(w)
import numpy
L1 = numpy.array(list(range(1, 32, 2)))
L2 = numpy.array(list(range(32, 63, 2)))
L3 = L1 + L2
print(L3)
import numpy
print((numpy.array(L1)>0).sum())
print((numpy.array(L2)>0).sum())
print((numpy.array(L3)>0).sum())
import numpy
L1 = numpy.array(L1)
L1[L1<0] = 0
L1[L1>100]=100
print(L1)
def flip_image(I):
return I[:,::-1,:]
import pygame
from pygame.locals import *
#initialize game window
screen = pygame.display.set_mode((600,600))
#import image resources
board = pygame.image.load('board.png')
circle = pygame.image.load('circle.png')
cross = pygame.image.load('cross.png')
#display board
screen.blit(board,(0,0))
pygame.display.flip()
#variables that store the state of the game
game_over = False
which_symbol = 'circle'
board_state = [[' ',' ',' '],
[' ',' ',' '],
[' ',' ',' ']]
line_width = 10
line_color = (0,0,0)
#function to check for matches in a straight line
def check_match():
#horizontal match
for i in range(3):
if not board_state[i][0]==' ' and board_state[i][0]==board_state[i][1]==board_state[i][2]:
line_start = (0, i*200+95)
line_end = (599, i*200+95)
pygame.draw.line(screen,line_color,line_start,line_end,line_width)
return True
#vertical match
for i in range(3):
if not board_state[0][i]==' ' and board_state[0][i]==board_state[1][i]==board_state[2][i]:
line_start = (i*200+95, 0)
line_end = (i*200+95, 599)
pygame.draw.line(screen,line_color,line_start,line_end,line_width)
return True
#left diagonal
if not board_state[0][0]==' ' and board_state[0][0]==board_state[1][1]==board_state[2][2]:
line_start = (0,0)
line_end = (599,599)
pygame.draw.line(screen,line_color,line_start,line_end,line_width)
return True
#right diagonal
if not board_state[2][0]==' ' and board_state[2][0]==board_state[1][1]==board_state[0][2]:
line_start = (599,0)
line_end = (0,599)
pygame.draw.line(screen,line_color,line_start,line_end,line_width)
return True
return False
#game loop
clock = pygame.time.Clock()
while True:
clock.tick(30)
if not game_over:
for event in pygame.event.get():
if event.type == MOUSEBUTTONUP:
mouse = pygame.mouse.get_pos()
grid_x = mouse[0] // 200
grid_y = mouse[1] // 200
print(board_state)
if board_state[grid_y][grid_x] == ' ':
if which_symbol=='circle':
board_state[grid_y][grid_x] = 'o'
X = grid_x * 200 + 25
Y = grid_y * 200 + 25
print('draw a circle at location',X,Y)
screen.blit(circle,(X,Y))
pygame.display.flip()
game_over = check_match()
pygame.display.flip()
which_symbol = 'cross'
elif which_symbol=='cross':
board_state[grid_y][grid_x] = 'x'
X = grid_x * 200 + 25
Y = grid_y * 200 + 25
print('draw a cross at location',X,Y)
screen.blit(cross,(X,Y))
game_over = check_match()
pygame.display.flip()
which_symbol = 'circle'
import pygame
print(pygame.__version__)
from pygame.locals import *
import random
screen = pygame.display.set_mode((500,500))
grass_image = pygame.image.load('grass.png')
grass_image = pygame.transform.scale(grass_image, (500,500))
apple_x = random.randint(0,400)
apple_y = random.randint(0,400)
apple_size = 50
apple = pygame.image.load('apple.png')
apple = pygame.transform.scale(apple, (apple_size,apple_size))
snake_x = random.randint(0,400)
snake_y = random.randint(0,400)
snake_direction = 'right'
snake_size = 100
snake = pygame.image.load('cobra.png')
snake = pygame.transform.scale(snake, (snake_size,snake_size))
def move_snake():
screen.blit(grass_image, (0,0))
screen.blit(apple, (apple_x,apple_y))
screen.blit(snake, (snake_x,snake_y))
pygame.display.flip()
clock = pygame.time.Clock()
while True:
clock.tick(30)
move_snake()
if snake_direction == 'right':
snake_x = snake_x + 5
elif snake_direction == 'left':
snake_x = snake_x - 5
elif snake_direction == 'up':
snake_y = snake_y - 5
elif snake_direction == 'down':
snake_y = snake_y + 5
if snake_x > apple_x - snake_size and snake_x < apple_x + apple_size and snake_y > apple_y - snake_size and snake_y < apple_y + apple_size:
print('apple consumed')
apple_x = random.randint(0,400)
apple_y = random.randint(0,400)
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_UP:
snake_direction = 'up'
elif event.key == K_DOWN:
snake_direction = 'down'
elif event.key == K_LEFT:
snake_direction = 'left'
elif event.key == K_RIGHT:
snake_direction = 'right'