[Tutor] (no subject)

Kent Johnson kent_johnson at skillsoft.com
Sat Sep 4 22:27:31 CEST 2004


I can see two good ways to do this. The simplest would be to represent a 
cliche as a tuple of (name, power) and keep a list of tuples to hold all 
the cliches. There would be one loop to input the cliches and another loop 
to print them. If the program is not going to get much bigger than what you 
posted, that is probably a good solution.

You could also make a class to represent a cliche. It would have attributes 
for name and power, a method to set its attributes from user input and a 
__str__() method to make a string representation for printing. This will be 
handy if your program is going to get bigger and make more use of cliches.

For a learning exercise you might even want to write the program both ways 
to get a better feel for the advantages of each one. My guess is the tuple 
approach will be shorter but using classes will give the program a very 
clean design and a better starting point for a larger program.

Kent

At 04:49 PM 9/4/2004 +0100, Adam Cripps wrote:
>----- Original Message -----
>From: Jon Kolbe <jonnorkol at yahoo.com>
>Date: Sat, 4 Sep 2004 08:40:26 -0700 (PDT)
>Here's my program
>
>#Risus Character Generator
>def risus():
>  cliches = {}
>  player = {}
>  player['Name'] = raw_input("What is the Character's name? ")
>  dice = input("How many dice will your character have? ")
>  player['Title'] = raw_input("What is the Character's title? ")
>  while dice > 0:
>   print "Do you want to add a cliche? "
>   print "1 Yes "
>   print "2 No "
>   reply = input()
>   if reply == 1:
>    print "What is the name of the cliche? "
>    reply = input()
>    cliches = { 'cliche': "reply" }
>    print "How many dice? "
>    power = input()
>    dice = dice-power
>    cliches = { 'cliche': "power" }
>    player[cliches] = power
>   else:
>    dice = 0
>  print "You're done!"
>  print character
>
>However, to me it sounds like your problem would better resolved with
>an object oriented approach. Does anyone else think this might suit
>Jon better?
>
>Adam
>_______________________________________________
>Tutor maillist  -  Tutor at python.org
>http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list