[Tutor] Newbie Project part 2...
Bob Gailer
bgailer at alum.rpi.edu
Sun Jul 15 23:05:05 CEST 2007
Here's a list-based alternative:
tree = [
["You have just turned 18 years old. Your life awaits,... choices
to be made..",
["Army",
["win Silver star",
["reenlist",
[] # next lower level...
],
["retire",
[]
]
],
["killed in action",
["burial",
[]
],
["cremation",
[]
]
]
],
["Navy",
["fall overboard",
["sink",
[],
],
["swim",
[]
]
],
["command battleship",
["be a hero",
[]
],
["undergo mutiny",
[]
]
]
]
]
]
import random
print "\t\t\t\tLife Simulator"
currentList = tree
while True:
currentList = random.choice(currentList)
print currentList[0]
question = ""
for position, item in enumerate(currentList[1:]):
question += "\n" + str(position + 1) + ") " + (item[0])
while True:
try:
ans = int(raw_input(question))
except KeyboardInterrupt:
break
except:
print "Please enter a number."
continue
if ans < 1 or ans > len(currentList):
print "Please enter a number between 1 and %s." % len(currentList)
continue
currentList = currentList[ans]
break
More information about the Tutor
mailing list