Re: [Tutor] Class methods

2011-06-26 Thread Marc Tompkins
On Sun, Jun 26, 2011 at 7:12 PM, David Merrick  wrote:

> Is it possible too have
>
> crit1 = Critter("Dave")
> crit2 = Critter("Sweetie")
> farm = [crit1,crit2]  #List#
>
> and then be able to use Critters methods on farm?
>
> No.  farm is a list, and lists don't inherit the methods of the objects
inside them (imagine what a nightmare _that_ would be, especially since
lists can contain more than one type of object at any given time!)

Instead, you would refer to the members of the list:
farm[0].talk()
farm[1].talk()
etc.  Only you wouldn't generally hard-code those numbers; instead, you
could use the "for x in y" style:
for monster in farm:
monster.talk()
At this point, "monster" is one of your Critters.  When I need a looping
variable for a collection of custom objects, I like to use words that are
synonymous with, or at least related to, the name of my custom class.  It
helps me keep track of what's going on - if I use x and y all over the
place, I tend to get confused.

You could also use a more traditional, less-Pythonic approach:
for x in len(farm):
farm[x].talk()
But seriously, why would you want to use Python to imitate Visual Basic?

By the way, you shouldn't ever need to refer to Dave and Sweetie by their
hard-coded variable names, so instead of instead of creating "crit1, crit2",
why not simply create them as members of the list?
farm = []
for critter_name in ["Dave", "Sweetie"]:
farm.append(Critter(critter_name))
Now the variables don't have names per se, but you can still refer to them
by number if you need one in particular, or you can just loop over the list
with "for monster in farm".

Finally, maybe a dictionary would be more useful/intuitive than a list?
farm = []
for critter_name in ["Dave", "Sweetie"]:
farm[critter_name] = Critter(critter_name)
farm["Dave"].talk()

I do think that "farm" is going to be a little misleading as a name for a
collection of Critters; might I suggest "herd" or "swarm"?  Just a
thought...
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Class methods

2011-06-26 Thread David Merrick
Is it possible too have

crit1 = Critter("Dave")
crit2 = Critter("Sweetie")
farm = [crit1,crit2]  #List#

and then be able to use Critters methods on farm?

# Critter Caretaker
# A virtual pet to care for

class Critter(object):

"""A virtual pet"""
def __init__(self, name, hunger = 0, boredom = 0):
self.name = name
self.hunger = hunger
self.boredom = boredom

# __ denotes private method
def __pass_time(self):
self.hunger += 1
self.boredom += 1
self.__str__()

def __str__(self):
print("Hunger is",self.hunger, "Boredom is " ,self.boredom)
print("Unhappines is ",self.hunger + self.boredom," and Mood is
",self.mood)



@property
def mood(self):
unhappiness = self.hunger + self.boredom
if unhappiness < 5:
m = "happy"
elif 5 <= unhappiness <= 10:
m = "okay"
elif 11 <= unhappiness <= 15:
m = "frustrated"
else:
m = "mad"
return m

def talk(self):
print("I'm", self.name, "and I feel", self.mood, "now.\n")
self.__pass_time()


def eat(self):
food = int(input("Enter how much food you want to feed your critter:
"))
print("Brruppp.  Thank you.")
self.hunger -= food
# hunger = 0 at iniatition
# self.hunger = self.boredom - food
if self.hunger < 0:
self.hunger = 0
self.__pass_time()


def play(self):
fun = int(input("Enter how much fun you want your critter to have:
"))
print("Wheee!")
self.boredom -= fun
# boredom = 0 at iniatition
# self.boredom = self.boredom - fun
if self.boredom < 0:
self.boredom = 0
self.__pass_time()


def main():
crit_name = input("What do you want to name your critter?: ")
crit = Critter(crit_name)

choice = None
while choice != "0":
print \
("""
Critter Caretaker

0 - Quit
1 - Listen to your critter
2 - Feed your critter
3 - Play with your critter
""")

choice = input("Choice: ")
print()

# exit
if choice == "0":
print("Good-bye.")

# listen to your critter
elif choice == "1":
crit.talk()

# feed your critter
elif choice == "2":
crit.eat()

# play with your critter
elif choice == "3":
crit.play()

# some unknown choice
else:
print("\nSorry, but", choice, "isn't a valid choice.")

main()
("\n\nPress the enter key to exit.")



-- 
Dave Merrick

merrick...@gmail.com

Ph   03 3423 121
Cell 027 3089 169
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Critter

2011-06-26 Thread Vincent Balmori

I'm on the Critter Caretake problem that involves handling more than one
critter. I have looked At David Merrick's threads for some answers, but the
difference is that he created a whole new class to handle it, while I only
made more critter objects. The only problem I have left is to have my
actions (play and eat) apply to all my creatures ALL AT ONCE, instead of one
at a time like it is in my code right now. 
http://old.nabble.com/file/p31933791/critter_caretaker3.py
critter_caretaker3.py 




-- 
View this message in context: 
http://old.nabble.com/Critter-tp31933791p31933791.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Television

2011-06-26 Thread Noah Hall
On Sun, Jun 26, 2011 at 8:28 PM, Vincent Balmori
 wrote:
>
> I made in elif statement for the channel changer that works for it, but the
> volume systems system boundary does not the way I want it to. If the volume
> is 2 and I lower it by a value of 5, it will accept the volume at a negative
> number thus going past the boundary. The vice-versa for the high boundary as
> well.
>
> http://old.nabble.com/file/p31932639/TV.py TV.py

In this case, you're testing in the wrong place again.

>def volume_down(tv, down = 1):

Again, no need for the variable "down" - you don't use it.

>if tv.volume > tv.volume_lowboundary :

tv.volume is always going to be less than tv.volume_lowboundary unless
set via calling tv.volume =  externally.

>down = int(input("\n How much do you want to lower the volume?: "))

Note you haven't got any error catching. What happens if I enter "mouse"?

>if down > 10:
>int(input("\n That's too much! Choose another number?: "))

Here you need an elif clause, checking if down is too low, for example,
elif tv.volume - down >1:

>else:
>tv.volume -= down
>print("\n The volume is now:", tv.volume)
>if tv.volume == 0:
>print("\nVolume is at lowest value.")

HTH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Television

2011-06-26 Thread Vincent Balmori

I made in elif statement for the channel changer that works for it, but the
volume systems system boundary does not the way I want it to. If the volume
is 2 and I lower it by a value of 5, it will accept the volume at a negative
number thus going past the boundary. The vice-versa for the high boundary as
well.

http://old.nabble.com/file/p31932639/TV.py TV.py 


Noah Hall-3 wrote:
> 
> On Sun, Jun 26, 2011 at 2:02 AM, Vincent Balmori
>  wrote:
>>
>> It's working better now. The problem I have left is that I have to set
>> the
>> channel and volume values in a range (for both I intend for 0-10). I
>> thought
>> the range() would be the choice, but probably I'm not using it right.
> 
> I think the best way to do this is probably to set two properties -
> tv.channel_boundary and tv.volume_boundary, this is presuming that you
> don't want to allow the lower boundary to be changed. If you do,
> create an upper and lower boundary variable for each.
> 
>>def channel_remote(tv, level = 1):
>>if tv.channel in range(0,10):
> 
> Here, it'd be better if you used
> if tv.channel < 10:
> 
> That way you're not wasting a whole list and a list search just to
> test if something is less than ten.
> Using the boundaries, that would be
> if tv.channel < tv.channel_boundary:
> 
> There's also no need for this, not here, anyway. You should be testing
> in __init__, where the channel can be set incorrectly.
> 
>>level = int(input("\n Which channel do you want to go up to:
>> "))
> 
> Why have this when you allow level as an argument? It's a waste.
> 
>>if 0 > level > 10:
>>int(input("\n That's not valid! Choose another channel:
>> "))
>>else:
>>tv.channel += level
> 
> This is your second problem you've found, I guess?
> Well, how about this
> tv.channel = level
> Easy, right? :)
> 
>>print("\n The channel is now:", tv.channel)
> 
> 
>> Another one is for the tv.channel to hold a new value instead of just
>> adding/subtracting the value and the only way I can think how is to use a
>> list and then have the function replace the value each time. I'm sure
>> there
>> is a better way though.
>> http://old.nabble.com/file/p31928968/TV TV
> 
> 
> HTH
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
> 
> 

-- 
View this message in context: 
http://old.nabble.com/Television-tp31925053p31932639.html
Sent from the Python - tutor mailing list archive at Nabble.com.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining a "format string"

2011-06-26 Thread Lisi
On Sunday 26 June 2011 17:15:14 Alan Gauld wrote:
> "Lisi"  wrote
> Umm, nearly...
> The format string contains format specifiers. They are not variables.
> The specifiers define the type of data to be inserted into the string
> as well as how the data will be formatted - hence the name.
>
> So you can specify how many decimal places for a float, whether
> numbers have leadig zeros, whether text is righ or left justified and
> more.

> HTH,

Definitely.  I am very grateful.  I have been unclear about this for some time 
and _think_that I have at last got it.  The things I asked questions about 
were the things I either knew or thought that I had got wrong.

I kept hoping that context would make things clear in the end, as indeed it 
often does.  But on this subject it clearly didn't.

So thanks for all the help.

Lisi


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining a "format string"

2011-06-26 Thread Alan Gauld

"Lisi"  wrote

So, if I have now understood correctly, a format string is a string 
containing
at least one variable, and the variable(s) is/are preceded by the % 
symbol.

Yes???


Umm, nearly...
The format string contains format specifiers. They are not variables.
The specifiers define the type of data to be inserted into the string
as well as how the data will be formatted - hence the name.

So you can specify how many decimal places for a float, whether
numbers have leadig zeros, whether text is righ or left justified and 
more.

(The new format specificiers in Python v3 are even more powerful)

You can store a format string as a variable (as Steven demonstrated)
and use it in several places to ensure consistent output. You can
swupply the values to the format string by using variables (again,
as Steven demonstrated). But they can also be literal values (as
Noah demonstrated) so that no variables are used at all.

You can also label format specifiers and then pass in a dictionary
of values where the labels are the keys into the dictionary. This is
very useful where the same values are used several times in a long
string. For example you could have a set of standard letter templates
into which you insert recipient data. You can then store the recipents
as a list of dictionaries ( or even a dictionary of dictionaries!) and 
then

do stuff like:

for recipient in customer_list:
print offer_letter % recipient

Here is a small example:


p = {'name':'Alan', 'balance': 100, 'duration': 300}
msg = "\nHello %(name)s,\nWe haven't heard from you for 
%(duration)s days.\nIf you come back we'll give you $10 off your 
next order."

print msg % p


Hello Alan,
We haven't heard from you for 300 days.
If you come back we'll give you $10 off your next order.




Notice I didn't need to use all the fields of p. The format
operation only extracted the fields named in the format
string.

It will even work with classes if you implement a __getitem__()
method

HTH,

--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Television

2011-06-26 Thread Noah Hall
On Sun, Jun 26, 2011 at 2:02 AM, Vincent Balmori
 wrote:
>
> It's working better now. The problem I have left is that I have to set the
> channel and volume values in a range (for both I intend for 0-10). I thought
> the range() would be the choice, but probably I'm not using it right.

I think the best way to do this is probably to set two properties -
tv.channel_boundary and tv.volume_boundary, this is presuming that you
don't want to allow the lower boundary to be changed. If you do,
create an upper and lower boundary variable for each.

>def channel_remote(tv, level = 1):
>if tv.channel in range(0,10):

Here, it'd be better if you used
if tv.channel < 10:

That way you're not wasting a whole list and a list search just to
test if something is less than ten.
Using the boundaries, that would be
if tv.channel < tv.channel_boundary:

There's also no need for this, not here, anyway. You should be testing
in __init__, where the channel can be set incorrectly.

>level = int(input("\n Which channel do you want to go up to: "))

Why have this when you allow level as an argument? It's a waste.

>if 0 > level > 10:
>int(input("\n That's not valid! Choose another channel: "))
>else:
>tv.channel += level

This is your second problem you've found, I guess?
Well, how about this
tv.channel = level
Easy, right? :)

>print("\n The channel is now:", tv.channel)


> Another one is for the tv.channel to hold a new value instead of just
> adding/subtracting the value and the only way I can think how is to use a
> list and then have the function replace the value each time. I'm sure there
> is a better way though.
> http://old.nabble.com/file/p31928968/TV TV


HTH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining a "format string"

2011-06-26 Thread Noah Hall
On Sun, Jun 26, 2011 at 2:42 PM, Lisi  wrote:
> Thanks, Noah and Steven.  :-)
>
> On Sunday 26 June 2011 12:24:12 Steven D'Aprano wrote:
>> Lisi wrote:
>> > In the following excerpt from a program in the book I am following:
>> >
>> >    print "If I add %d, %d, and %d I get %d." % (
>> >       my_age, my_height, my_weight, my_age + my_height + my_weight)
>> >
>> > is
>> >
>> > % (
>> >       my_age, my_height, my_weight, my_age + my_height + my_weight)
>> >
>> > the/a format string?
>>
>> No. The format string is a string with the % codes. In this case, they
>> are all %d codes:
>>
>> "If I add %d, %d, and %d I get %d."
>>
>> but there are other codes possible.
>>
>> The % symbol on its own is an operator, like + or * or /
>>
>> The part inside the brackets () is a tuple of values to insert into the
>> format string. Putting the three together:
>>
>>
>> target = "Hello %s."
>> value = "Lisi"
>> print target % value
>>
>> => prints "Hello Lisi."
>
> At least I had managed to pick up correctly that the format string needed a %
> symbol!
>
> So, if I have now understood correctly, a format string is a string containing
> at least one variable, and the variable(s) is/are preceded by the % symbol.

Well, sort of. Leaning more to the "no" side, but sort of.

A format string is a string containing specifiers (placeholders, if
you will) for values, including variables, but not the values
themselves.

The % symbol is a specifier. So, for example, %d specifies that
something should be of type d which, in Python, is an int.

The actual variables/values are the bit after the format string, and
after the %.

HTH.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining a "format string"

2011-06-26 Thread Lisi
Thanks, Noah and Steven.  :-)

On Sunday 26 June 2011 12:24:12 Steven D'Aprano wrote:
> Lisi wrote:
> > In the following excerpt from a program in the book I am following:
> >
> >print "If I add %d, %d, and %d I get %d." % (
> >   my_age, my_height, my_weight, my_age + my_height + my_weight)
> >
> > is
> >
> > % (
> >   my_age, my_height, my_weight, my_age + my_height + my_weight)
> >
> > the/a format string?
>
> No. The format string is a string with the % codes. In this case, they
> are all %d codes:
>
> "If I add %d, %d, and %d I get %d."
>
> but there are other codes possible.
>
> The % symbol on its own is an operator, like + or * or /
>
> The part inside the brackets () is a tuple of values to insert into the
> format string. Putting the three together:
>
>
> target = "Hello %s."
> value = "Lisi"
> print target % value
>
> => prints "Hello Lisi."

At least I had managed to pick up correctly that the format string needed a % 
symbol!

So, if I have now understood correctly, a format string is a string containing 
at least one variable, and the variable(s) is/are preceded by the % symbol.  
Yes???

Lisi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining a "format string"

2011-06-26 Thread Steven D'Aprano

Lisi wrote:

In the following excerpt from a program in the book I am following:

   print "If I add %d, %d, and %d I get %d." % ( 
  my_age, my_height, my_weight, my_age + my_height + my_weight)


is 

% ( 
  my_age, my_height, my_weight, my_age + my_height + my_weight)


the/a format string?



No. The format string is a string with the % codes. In this case, they 
are all %d codes:


"If I add %d, %d, and %d I get %d."

but there are other codes possible.

The % symbol on its own is an operator, like + or * or /

The part inside the brackets () is a tuple of values to insert into the 
format string. Putting the three together:



target = "Hello %s."
value = "Lisi"
print target % value

=> prints "Hello Lisi."



--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Defining a "format string"

2011-06-26 Thread Noah Hall
On Sun, Jun 26, 2011 at 12:05 PM, Lisi  wrote:
> In the following excerpt from a program in the book I am following:
>
>   print "If I add %d, %d, and %d I get %d." % (
>      my_age, my_height, my_weight, my_age + my_height + my_weight)
>
> is
>
> % (
>      my_age, my_height, my_weight, my_age + my_height + my_weight)
>
> the/a format string?

No.
(my_age, my_height, my_weight, my_age + my_height + my_weight)
are the values (variables in this case, but it could be direct values too)
"If I add %d, %d, and %d I get %d."
is the formatted string (what you want to see outputted as a string)

So, say we had -
my_age = 65
my_height = 185
my_weight = 11

Then what'd happen is -
print "If I add %d, %d, and %d I get %d." % (65, 185, 11, 65 + 185 + 11)

Notice how the values are actual values? The other text part is the
format for the values to take inside a string, so you get -
print "If I add 65, 185 and 11 I get 261"


HTH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Defining a "format string"

2011-06-26 Thread Lisi
In the following excerpt from a program in the book I am following:

   print "If I add %d, %d, and %d I get %d." % ( 
  my_age, my_height, my_weight, my_age + my_height + my_weight)

is 

% ( 
  my_age, my_height, my_weight, my_age + my_height + my_weight)

the/a format string?

If not, then I still haven't "got" it, and I would be very grateful if someone 
could define "format string" for me - preferably in words of one syllable, 
because I seem to be being a bit thick about this. :-(

Lisi

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor