Codecademy: Python

badge_Listsbadge_Taking_a_vacationbadge_Functionsbadge_PygLatinCodecademy: Python
http://www.codecademy.com/tracks/python

Learn the fundamentals of programming to build web apps and manipulate data.

MS Word > show or hide formatting marks for spaces:
http://www.itechtalk.com/thread7639.html

Text comparison:
http://www.textdiff.com

Floating point arithmetic: Issues and Limitations
https://docs.python.org/3.4/tutorial/floatingpoint.html

===========================

from random import randint

board = []

for x in range(5):
board.append([“O”] * 5)

def print_board(board):
for row in board:
print ” “.join(row)

print “Let’s play Battleship!”
print_board(board)

def random_row(board):
return randint(0, len(board) – 1)

def random_col(board):
return randint(0, len(board[0]) – 1)

ship_row = random_row(board)
ship_col = random_col(board)
print ship_row
print ship_col

# Everything from here on should go in your for loop!
# Be sure to indent four spaces!
for turn in range(4):
# Print (turn + 1) here!
print(“Turn”, turn + 1)
guess_row = int(raw_input(“Guess Row: “))
guess_col = int(raw_input(“Guess Col: “))

if guess_row == ship_row and guess_col == ship_col:
print “Congratulations! You sunk my battleship!”
break
else:
if (guess_row < 0 or guess_row > 4) or \
(guess_col < 0 or guess_col > 4):
print(“Oops, thats not even in the ocean.”)
elif (board[guess_row][guess_col] == “X”):
print “You guessed that one already.”
else:
print “You missed my battleship!”
if turn == 3:
print(“Game Over”)
board[guess_row][guess_col] = “X”
print_board(board)

====================

import random
>>> def name_to_number(name):
# convert name to number using if/elif/else
if name == “rock”:
numbercode = 0
elif name == “Spock”:
numbercode = 1
elif name == “paper”:
numbercode = 2
elif name == “lizard”:
numbercode = 3
else:
numbercode = 4
return numbercode

>>> def number_to_name(number):
# convert number to a name using if/elif/else
if number == 0:
nameis = “rock”
elif number == 1:
nameis = “Spock”
elif number == 2:
nameis = “paper”
elif number == 3:
nameis = “lizard”
else:
nameis = “scissors”
return nameis

def rpsls(player_choice):

# print a blank line to separate consecutive games
print (” “)

# print out the message for the player’s choice
print( “Player chooses”, player_choice)

# convert the player’s choice to player_number using the function name_to_number()
player_number = name_to_number(player_choice)

# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(1, 5)

# convert comp_number to comp_choice using the function number_to_name()
comp_choice = number_to_name(comp_number)

# print out the message for computer’s choice
print( “Computer chooses”, comp_choice)

# compute difference of comp_number and player_number modulo five
difference_cp = (comp_number – player_number) % 5

# use if/elif/else to determine winner, print winner message
if difference_cp == 0:
print( “Game is tied”)
elif difference_cp <= 2:
print( “Computer wins!”)
else:
print( “Player wins!”)

==================================

(Oct. 17, 2015)

import random

# helper functions

def name_to_number(name):
if name == ‘rock’:
return 0
elif name == ‘Spock’:
return 1
elif name == ‘paper’:
return 2
elif name == ‘lizard’:
return 3
else:
return 4 #scissors

# convert name to number using if/elif/else
# don’t forget to return the result!

def number_to_name(number):
if number == 0:
return ‘rock’
elif number == 1:
return ‘Spock’
elif number == 2:
return ‘paper’
elif number == 3:
return ‘lizard’
else:
return ‘scissors’

# convert number to a name using if/elif/else
# don’t forget to return the result!

def rpsls(player_choice):

# print a blank line to separate consecutive games
print “”

# print out the message for the player’s choice
print “Player chooses”, player_choice #guess by the user

# convert the player’s choice to player_number using the function name_to_number()
player_number = name_to_number(player_choice)

# compute random guess for comp_number using random.randrange()
comp_number = random.randrange(0, 5)

# convert comp_number to comp_choice using the function number_to_name()
computer_choice = number_to_name(comp_number)

# print out the message for computer’s choice
print “Computer chooses”, computer_choice

# compute difference of comp_number and player_number modulo five
difference = (player_number – comp_number) % 5

# use if/elif/else to determine winner, print winner message
if difference == 0:
result = “Player and computer tie!”
elif difference == 1 or difference == 2:
result = “Player wins!”
else:
result = “Computer wins!”
print result

============================================

http://python-alhindi.blogspot.com

=============================================

def factorial(n):
if n==1:
return 1
else:
return n * factorial(n-1)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s