Fun with python

 

I have been learning python via numerous resources,  codeacademy and more recently with the ubuntu full circle special edition,  issue 1 course on python. (see my programming section for links etc)

Thus far with some help from Tom Brough I have come up with this.,  its not much it takes input as 2 strings, and does various things with them. and also works out my age from entering my year of birth and subtracting this from the current year.

#!/usr/bin/env python

# 24.03.2013

from datetime import datetime
import string
now = datetime.now()

print “current year = “, + now.year

firstname = raw_input(“what is your firstname? “)
surname = raw_input(“what is your surname? “)
birthyear = raw_input(“what year were you born in ? ” )
sp = ” ”
print birthyear
print now.year
print
print “hello, ” + firstname + sp + surname
print
print “your name length ”
print len(firstname) + len(surname)
#convert ascii input to a integer
birthyear = string.atoi(birthyear)
#perform operation on the integer
age = now.year- birthyear
print “your age is”, age

 

One of the advantages with using different resources is that you can experiment and try and improve software as you go along,

 

for example I have modified the dragon.py program as found in the inventwithpython book and changed the input method to raw_input, this gets round having to use quotes “” when entering the cave number.
import random
import time

def displayIntro():
print(‘You are in a land full of dragons. In front of you,’)
print(‘you see two caves. In one cave, the dragon is friendly’)
print(‘and will share his treasure with you. The other dragon’)
print(‘is greedy and hungry, and will eat you on sight.’)
print(‘enter responses in quotes ” ” ‘)
print()

#use raw_input to make the input user friendly

def chooseCave():
cave = ”
while cave != ‘1’ and cave != ‘2’:
print(‘Which cave will you go into? (1 or 2 ‘ )
cave = raw_input(“Cave “)

return cave

#sleep is set to 1 rather than 2 so game is quicker

def checkCave(chosenCave):
print(‘You approach the cave…’)
time.sleep(1)
print(‘It is dark and spooky…’)
time.sleep(1)
print(‘A large dragon jumps out in front of you! He opens his jaws and…’)
print()
time.sleep(1)

friendlyCave = random.randint(1, 2)

if chosenCave == str(friendlyCave):
print(‘Gives you his treasure!’)
else:
print(‘Gobbles you down in one bite!’)

playAgain = ‘yes’
while playAgain == ‘yes’ or playAgain == ‘y’:

displayIntro()

caveNumber = chooseCave()

checkCave(caveNumber)

print(‘Do you want to play again? (yes or no)’)
playAgain = raw_input(“Play Again y / n “)

 

If you are using codeacademy then the PySchools Quick Reference website is a good way to remember some of the concepts being taugth print pages off for future reference or just save them somewhere, just don’t open the page and end up closing the code academy page at the same time.

I will keep playing.