Two non-trivial examples chosen at random:
Days2
Code: Select all
# Days 2
# Calculates day of week
monnum = [0,3, 3, 6, 1, 4, 6, 2, 5, 0, 3, 5]
day = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
while True:
date = int(raw_input("Please enter the day of the month: "))
mon = int(raw_input("Now enter the month number(1-12): "))
year = int(raw_input("Now enter the year: "))
if year < 40: year += 100
if year < 140: year += 1900
if year < 1900 or year > 2099: print "Sorry, this program only works with years 1900 to 2099"; continue
year -= 1900
leaps = year // 4
if year % 4 == 0 and mon < 3 and year > 0: leaps -= 1
total = year + leaps + monnum[mon-1] + date
total = total % 7
print "That day was a", day[total]
char = raw_input("Do you want another go (y/n): ")
print
if char[0] != "Y" and char[0] != "y": break
There's no READ/DATA statements in Python, but I think the solution above is clearer than BASIC. Python indexes start at zero, otherwise the above is a faithful rendition of the (execrable) BASIC code. BASIC has two UNTIL statements that break the structured paradigm, but Python lacks REPEAT-UNTIL. I'd call that one all.
Boxes
Code: Select all
# Boxes
# Creates rectangle to show different colours
import sys
import pygame
pygame.init()
window = pygame.display.set_mode((640, 480))
BBC_COLOUR=[(0,0,0), (255,0,0), (0,255,0), (255,255,0), (0,0,255),
(255,0,255), (0,255,255), (255,255,255)]
for colour in range(7,-1,-1):
rectangle = pygame.Rect(40+colour*20, 80+colour*20, 120+colour*12, 100+colour*10)
pygame.draw.rect(window, BBC_COLOUR[colour], rectangle)
pygame.draw.circle(window, pygame.Color("white"), (360, 150), 300, 2)
pygame.display.flip()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(0)
Note that pygame uses the full colour-space of the screen, while the example BASIC only uses indexed 8-colour mode, so I had to work hard to generate the right colours (which don't agree with the RaspPi implementation.) Also the coordinate systems don't agree and I couldn't be bothered to research and re-jig the maths too much. There's a certain amount of boiler-plate code at the end to keep the screen open and visible.
Back at you: convert this to BASIC:
Code: Select all
ages = {}
ages['Sue'] = 23
ages['Peter'] = 19
ages['Andrew'] = 78
ages['Karen'] = 45
print "The dictionary has", len(ages), "entries in it"
if ages.has_key('Sue'):
print "Sue is in the dictionary. She is", ages['Sue'], "years old"
else:
print "Sue is not in the dictionary"
names = ages.keys()
print "The following people are in the dictionary:"
print names
print "and in alphabetical order:"
names.sort()
for name in names:
print " ", name, "is", ages[name], "years old"