Re: [Tutor] Exercise to work on

2014-08-14 Thread P McCombs
On Aug 12, 2014 3:29 PM, "keith papa"  wrote:
>
> Hi, am a newbie to python and I wondering if you guys can give me some
exercise to work on. I have learned: print function , if function ,
strings, variables, and raw_input. The more the better

Checkio.org is a game made of python coding challenges. They start simple,
demonstrate functional testing, and provide other users' solutions.

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


Re: [Tutor] Exercise to work on

2014-08-13 Thread Greg Markham
Keith,

This should get you started...

http://learnpythonthehardway.org/book/

http://www.codecademy.com/en/tracks/python

http://docs.python-guide.org/en/latest/intro/learning/

Happy coding!

--Greg


On Tue, Aug 12, 2014 at 1:52 PM, keith papa  wrote:

> Hi, am a newbie to python and I wondering if you guys can give me some
> exercise to work on. I have learned: print function , if function ,
> strings, variables, and raw_input. The more the better
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise to work on

2014-08-13 Thread Joseph Lee

Hi Keith,
It depends on what you want to do with Python in the end (by the 
way, if statement is not a function).


Here are some of my suggestions (based on current topics and 
things to come later):


1.  Write a loop which prints even numbers from 1 to 100.

2.  Write a program which determines if the entered number is an 
even or odd number.


3.  Given a string, locate the last occurrence of a character.

4.  Write a program that says number of characters in a string 
and whether it starts with a consonant or a vowel.


I think that's it for now.  Again the rest of the exercises that 
might show up might depend on your ultimate goal with Python (by 
the way, which version are you using?).  Other topics to learn 
about would be how to write functions, reading from a file, while 
and for loops and working with organized data such as lists and 
dictionaries.


Cheers,
Joseph
- Original Message -
From: keith papa Hi, am a newbie to python and I wondering if you guys can give me 
some exercise to work on.  I have learned: print function , if 
function , strings, variables, and raw_input.  The more the 
better

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


Re: [Tutor] Exercise to work on

2014-08-13 Thread Joseph Lee

Hi Keith,
As a follow-up: I'm sure others might have mentioned this, but I 
also recommend doing exercises presented in the book or reference 
you're using to learn Python.

Cheers,
Joseph

- Original Message -
From: keith papa Hi, am a newbie to python and I wondering if you guys can give me 
some exercise to work on.  I have learned: print function , if 
function , strings, variables, and raw_input.  The more the 
better

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


Re: [Tutor] Exercise to work on

2014-08-12 Thread Danny Yoo
On Tue, Aug 12, 2014 at 1:52 PM, keith papa  wrote:
> Hi, am a newbie to python and I wondering if you guys can give me some
> exercise to work on. I have learned: print function , if function , strings,
> variables, and raw_input. The more the better

Do you find yourself doing certain kinds of computations?  A typical
first beginner example is to compute a fahrenheit to celsius
calculator.   Numbers are usually easy to work with, so problems that
automate basic computations are prevalent in beginner exercises.  I'd
recommend trying exercises in a beginner's textbook; those are usually
fairly easy to do and useful.

For example:

http://www.greenteapress.com/thinkpython/html/thinkpython003.html#toc23


---


Besides that, do you have hobbies or interests outside of computer
programming?  Maybe there's something there that you can approach.
When I'm learning a language, I try to do something personal to make
it concrete, useful, and fun.  For example (though not related to
Python programming), when I was learning Golang, I tried:

https://plus.google.com/117593568781545916065/posts/CALjoYeKU7r

because was in the middle of trying to figure out how marginal taxes
work, having read an article and not quite getting it.  Writing that
program let me learn both something domain-specific as well as get a
feel for a different programming language.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Exercise to work on

2014-08-12 Thread keith papa
Hi, am a newbie to python and I wondering if you guys can give me some exercise 
to work on. I have learned: print function , if function , strings, variables, 
and raw_input. The more the better ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise (while loop)

2014-04-01 Thread Danny Yoo
> So this call will always try to round None(the default return value)
> And of course it produces no output since it prints nothing.
>
> Are you sure that's actually what is in the book?


No.  That's very much why I wanted a reference to the original source
of the problem.

Scott attributed too much to the book when he presented the problem.
In the original content,

http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81

it simply presents a running dialogue exploring the idea of computing
square roots iteratively, culminating in a toplevel for-loop that
simply prints out its improving guess.  There is no function there.

This is why we want to be a bit more careful when saying "The book
said this..." following up with a paraphrase, because sometimes we can
get the paraphrasing wrong.  Similarly issues occur when one is
presenting error message content and asking for debugging advice.
Pointing to primary sources is usually a good idea, especially when
debugging or trying to get at root causes.


Let's head-off this sort of confusion quickly next time.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise (while loop)

2014-04-01 Thread Alan Gauld

On 01/04/14 02:07, Scott W Dunning wrote:

I’m working on a few exercises and I’m a little stuck on this one.

This is what the book has but it just gives me an endless loop.

def square_root(a, eps=1e-6):
while True:
print x
y = (x + a/x) / 2
if abs(y-x) < epsilon:
break



Are you sure that's what the book has?
If so I'd consider another book. That code is seriously broken.
- It prints x before an x is defined.
- It never modifies x and so the abs(y-x) will always be the
  same so the loop never breaks.
- it tests for epsilon but has eps as parameter
- It also never returns any value from the function.


round(square_root(9))


So this call will always try to round None(the default return value)
And of course it produces no output since it prints nothing.

Are you sure that's actually what is in the book?


I tweaked it to what I thought was correct but when I test it I get nothing 
back.

def square_root(a, eps=1e-6):
x = a/2.0
while True:
y = (x + a/x)/2.0
if abs(x - y) < eps:
return y
x = y


This is slightly better than the above, at least it creates an x and 
modifies it and returns a value.


And it seems to work on my system. How did you test it?


round(square_root(9))


If you used the >>> prompt this would have produced a result but if you 
used a script file you would need to print it.



HTH
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos

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


Re: [Tutor] exercise (while loop)

2014-04-01 Thread Dave Angel
 Scott W Dunning  Wrote in message:
> I’m working on a few exercises and I’m a little stuck on this one.  
> 
> This is what the book has but it just gives me an endless loop.
> 
> def square_root(a, eps=1e-6):
>   while True:
>   print x
>   y = (x + a/x) / 2
>   if abs(y-x) < epsilon:
>   break
> 

Without an initial value for x, this should give an immediate
 exception.   Assuming you fix that as below,  you now have the
 problem that they never change x, so if it isn't right on first
 loop, it never will be. Next you have the problem of inconsistent
 spelling of eps. And final thing I notice is that it doesn't
 return a value.  Once you remove the debug print in the function,
  there's no way to see the result. 


> round(square_root(9))
> 
> I tweaked

Good job, you fixed most of the bugs. 

> it to what I thought was correct but when I test it I get nothing back.
> 
> def square_root(a, eps=1e-6):
>x = a/2.0
>while True:
>y = (x + a/x)/2.0
>if abs(x - y) < eps:
>return y
>x = y
> 
> round(square_root(9))
> 
> The way I tweaked it seems to work, I’m getting the correct answer on the 
> calculator but the interpreter is not returning anything when I check in 
> python.

Sure it is, you're just not printing it. You forgot to save the
 result of rounding,  and forgot to print the saved
 value.



-- 
DaveA

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


Re: [Tutor] exercise (while loop)

2014-04-01 Thread Mark Lawrence

On 01/04/2014 02:47, Danny Yoo wrote:


On Mar 31, 2014 6:22 PM, "Scott W Dunning" mailto:scott@cox.net>> wrote:
 >
 > I’m working on a few exercises and I’m a little stuck on this one.
 >
 > This is what the book has but it just gives me an endless loop.
 >
 > def square_root(a, eps=1e-6):
 > while True:
 > print x
 > y = (x + a/x) / 2
 > if abs(y-x) < epsilon:
 > break
 >
 > round(square_root(9))

Hi Scott,

Ah.  I think I see what might be wrong, but let's make sure about this.

Can you explain what 'x', 'y' are in this function?



And the difference between eps and epsilon while (ouch) we're at it.

--
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.


Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


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


Re: [Tutor] exercise (while loop)

2014-03-31 Thread Scott W Dunning

On Mar 31, 2014, at 7:10 PM, Danny Yoo  wrote:
Thanks for the info Danny!  I’ll try that and I should be able to figure it out 
with your help!  

The book I was referring to is greentreepress.

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


Re: [Tutor] exercise (while loop)

2014-03-31 Thread Danny Yoo
On Mon, Mar 31, 2014 at 8:48 PM, Scott W Dunning  wrote:
>
> On Mar 31, 2014, at 7:10 PM, Danny Yoo  wrote:
> Thanks for the info Danny!  I’ll try that and I should be able to figure it 
> out with your help!
>
> The book I was referring to is greentreepress.


The reason I'm asking is I want to double check the example code.


Checking...

http://greenteapress.com/

... but Green Tree Press publishes a few Python books.  Hmmm.  I will
guess that you mean: Allen Downey's: "How to Think Like a Computer
Scientist".


Ah, found it.

http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81

But please, try to provide details.  You tend to suppress helpful
details.  I would like to avoid guessing next time, so be aware that
we don't see what you're thinking.



Ok, I see now what you were looking at.  But we need to wheel back
around to one of your original questions.  You said:

> This is what the book has but it just gives me an endless loop.
>
> def square_root(a, eps=1e-6):
> while True:
> print x
> y = (x + a/x) / 2
> if abs(y-x) < epsilon:
>break
>
> round(square_root(9))


Go back and look at that text again:

http://greenteapress.com/thinkpython/html/thinkpython008.html#toc81

and now see that the book does not present a function in that section.
 Instead, it's showing exploratory code.  There's no function there,
all the state is global, and it's not computing a return value.

So you shouldn't be too surprised that the code the book is
presenting, as a non-functional example, requires some adaptation
before it works as a function.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise (while loop)

2014-03-31 Thread Danny Yoo
> I tweaked it to what I thought was correct but when I test it I get nothing 
> back.
>
> def square_root(a, eps=1e-6):
>x = a/2.0
>while True:
>y = (x + a/x)/2.0
>if abs(x - y) < eps:
>return y
>x = y
>
> round(square_root(9))
>
> The way I tweaked it seems to work, I’m getting the correct answer on the 
> calculator but the interpreter is not returning anything when I check in 
> python.


I didn't want to keep you waiting, so I'll cut to the chase.  This
line here in your program:

round(square_root(9))

computes a value... But it doesn't do anything with that value.

Try printing the value.


You may also try to see that your program is doing something effective
by "unit testing" it.  This is often a lot better than just printing
values and looking at them, because the test case will say what the
_expected_ value is, so it's more informative.



For this example, the following is a start at unit testing the above
function.  Add the following to the bottom of your program's source.


###
## See:  http://www.openp2p.com/pub/a/python/2004/12/02/tdd_pyunit.html
import unittest
class SquareRootTests(unittest.TestCase):
def testSimpleCases(self):
self.assertAlmostEqual(square_root(1), 1.0)
self.assertAlmostEqual(square_root(4), 2.0)

if __name__ == '__main__':
unittest.main()
###


Here's what it looks like when I run this:

##
$ python sqrt.py
4.472135955
.
--
Ran 1 test in 0.000s

OK
##


You can then start adding more and more to tests to gain confidence
that the code is doing something reasonable.



If we try to put in an intentionally broken test, like:

self.assertAlmostEqual(square_root(3), 2.0)

in the body of testSimpleCases(), then we'll see the following error
when running the program:


##
$ python sqrt.py
4.472135955
F
==
FAIL: testSimpleCases (__main__.SquareRootTests)
--
Traceback (most recent call last):
  File "sq.py", line 20, in testSimpleCases
self.assertAlmostEqual(square_root(3), 2.0)
AssertionError: 1.7320508075688772 != 2.0 within 7 places

--
Ran 1 test in 0.000s

FAILED (failures=1)
##


And that's what you want to see.  If either the test or the code is
bad, it'll say something about it.


One other thing: you will want to check a particularly insidious case
that will cause the program here to behave badly.  Consider the zero
case: square_root(0).  Write the test case.  Run it.  You'll see
something interesting.



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


Re: [Tutor] exercise (while loop)

2014-03-31 Thread Danny Yoo
Also, which book?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise (while loop)

2014-03-31 Thread Danny Yoo
On Mar 31, 2014 6:22 PM, "Scott W Dunning"  wrote:
>
> I’m working on a few exercises and I’m a little stuck on this one.
>
> This is what the book has but it just gives me an endless loop.
>
> def square_root(a, eps=1e-6):
> while True:
> print x
> y = (x + a/x) / 2
> if abs(y-x) < epsilon:
> break
>
> round(square_root(9))

Hi Scott,

Ah.  I think I see what might be wrong, but let's make sure about this.

Can you explain what 'x', 'y' are in this function?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] exercise (while loop)

2014-03-31 Thread Scott W Dunning
I’m working on a few exercises and I’m a little stuck on this one.  

This is what the book has but it just gives me an endless loop.

def square_root(a, eps=1e-6):
while True:
print x
y = (x + a/x) / 2
if abs(y-x) < epsilon:
break

round(square_root(9))

I tweaked it to what I thought was correct but when I test it I get nothing 
back.

def square_root(a, eps=1e-6):
   x = a/2.0
   while True:
   y = (x + a/x)/2.0
   if abs(x - y) < eps:
   return y
   x = y

round(square_root(9))

The way I tweaked it seems to work, I’m getting the correct answer on the 
calculator but the interpreter is not returning anything when I check in python.

The books way is just print whatever I use for x, so I don’t understand that at 
all.


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


Re: [Tutor] exercise

2012-12-14 Thread Matthew Ngaha
> return {e for (e, g) in self.sort_email.items()
> if g & groups_list}
>

guys i think ive got it. The & in that comprehension was really
confusing me, but i found out it means intersection, so i took the
sets manually and saw the results i got using intersection and it
became more clear from there. A Big Thanks if you took the time to
help solve my issues.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes 2nd attempt

2012-02-12 Thread Brian van den Broek
On 12 Feb 2012 05:23, "Tonu Mikk"  wrote:
>
> I am learning Python using the "Learn Python the Hard Way" book by Zed
Shaw.  I reached exercise 42 where we learn about Python classes.  The
exercise shows a game with one class that includes all the definitions for
playing the game.  For extra credit we are asked to create another version
of this game where we use two classes - one for the room definitions and
the other for the playing the game.
>
> May attempt at creating two classes is here http://codepad.org/963vUgSt .
 I get the following error which I have been un-able to resolve.  Any
suggestions are welcome.
>
> Traceback (most recent call last):
>   File "ex42_3.py", line 233, in 
> run()
>   File "ex42_3.py", line 231, in run
> room1.doIt()  # plays the game
>   File "ex42_3.py", line 32, in doIt
> self.obj.play() # use object
>   File "ex42_3.py", line 20, in play
> room = getattr(self, next)
> AttributeError: 'Engine' object has no attribute 'central_corridor'

Hi,

Your code is longer than I feel like reading carefully. (Not a complaint;
just cautioning you about how closely I looked.) Also, the line numbers of
your code sample do not agree with those of your traceback. (That is a mild
complaint; it makes it harder to help you.)

Notice that you defined central_corridor as a method of Room. The last line
of your traceback is (it seems) in Engine.play; the code there looks for
central_corridor in Engine and doesn't find it.

If that help, great. If not, try to trim down your code to a smaller
version that exhibits the problem and post again, this time making sure the
posted code and the code that generate the traceback are the same.

Best,

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


[Tutor] exercise with classes 2nd attempt

2012-02-11 Thread Tonu Mikk
I am learning Python using the "Learn Python the Hard Way" book by Zed
Shaw.
 I reached exercise 42 where we learn about Python
classes.
 The exercise shows a game with one class that includes all the definitions
for playing the game.  For extra credit we are asked to create another
version of this game where we use two classes - one for the room
definitions and the other for the playing the game.

May attempt at creating two classes is here http://codepad.org/963vUgSt .
 I get the following error which I have been un-able to resolve.  Any
suggestions are welcome.

Traceback (most recent call last):
  File "ex42_3.py", line 233, in 
run()
  File "ex42_3.py", line 231, in run
room1.doIt()  # plays the game
  File "ex42_3.py", line 32, in doIt
self.obj.play() # use object
  File "ex42_3.py", line 20, in play
room = getattr(self, next)
AttributeError: 'Engine' object has no attribute 'central_corridor'

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


Re: [Tutor] exercise with classes

2012-02-09 Thread Joel Goldstick
On Thu, Feb 9, 2012 at 2:23 PM, Tonu Mikk  wrote:
>
>
> On Mon, Feb 6, 2012 at 12:58 PM, Dave Angel  wrote:
>>
>> On 02/06/2012 01:24 PM, Tonu Mikk wrote:
>>>
>>> Now I get an error:  NameError: global name 'self' is not define.
>>>
>>> Tonu
>>>
>>>
>> Put your remarks after the stuff you quote.  You're top-posting, which
>> makes the reply difficult to follow.
>>
>> Use copy/paste to describe an error message.  You retyped the one above,
>> and added a typo.  Include the whole error, which includes the stack trace.
>>
>> If you had followed Nate's advice, you couldn't have gotten that error at
>> all, so you'll also need to post the code that actually triggers the error.
>
>
> Let's try this one more time.  I thought that I would try to make Alan's
> code work.  Here is the version what I have now:
>
> class Printer:
>   def __init__(self,number=0):
>      self.value = number
>   def sayIt(self):
>      print self.value
>
> class MyApp:
>   def __init__(self, aPrinter = None):
>       if aPrinter == None:     # if no object passed create one
>          aPrinter = Printer()
>       self.obj = aPrinter      # assign object
>   def doIt():
>       self.obj.sayIt()         # use object
>
> def test():
>   p = Printer(42)
>   a1 = MyApp()
>   a2 = MyApp(p)   # pass p object into a2
>   a1.doIt(self)   # prints default value = 0
>   a2.doIt(self)   # prints 42, the value of p

I haven't run this, but I think this is what is going on here:

When you make the call to the method you don't include self.   You
have no self variable in your namespace, so it throws the error.
However, when you write a method in a class, you need to declare the
first argument self (its a convention to call it self.  You could call
it 'me', or anything -- but don't).  self is the object itself.

So do this:
   a1.doIt()
   a2.doIt()

Alternately you could do this:
   MyApp.doIt(a1)

In this case you are saying use the object a1 which is  of the MyApp
Class.  But this looks weird
>
> test()
>
> When I run it, I get an error:
> Traceback (most recent call last):
>   File "alan_class.py", line 22, in 
>     test()
>   File "alan_class.py", line 19, in test
>     a1.doIt(self)   # prints default value = 0
> NameError: global name 'self' is not defined
>
>
>>
>>
>>
>>
>>
>>
>>
>> --
>>
>> DaveA
>>
>
>
>
> --
> Tonu Mikk
> Disability Services, Office for Equity and Diversity
> 612 625-3307
> tm...@umn.edu
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



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


Re: [Tutor] exercise with classes

2012-02-09 Thread Dave Angel

On 02/09/2012 02:23 PM, Tonu Mikk wrote:

On Mon, Feb 6, 2012 at 12:58 PM, Dave Angel  wrote:


On 02/06/2012 01:24 PM, Tonu Mikk wrote:


Now I get an error:  NameError: global name 'self' is not define.

Tonu


  Put your remarks after the stuff you quote.  You're top-posting, which

makes the reply difficult to follow.

Use copy/paste to describe an error message.  You retyped the one above,
and added a typo.  Include the whole error, which includes the stack trace.

If you had followed Nate's advice, you couldn't have gotten that error at
all, so you'll also need to post the code that actually triggers the error.



Let's try this one more time.  I thought that I would try to make Alan's
code work.  Here is the version what I have now:

class Printer:
   def __init__(self,number=0):
  self.value = number
   def sayIt(self):
  print self.value

class MyApp:
   def __init__(self, aPrinter = None):
   if aPrinter == None: # if no object passed create one
  aPrinter = Printer()
   self.obj = aPrinter  # assign object
   def doIt():
   self.obj.sayIt() # use object

def test():
   p = Printer(42)
   a1 = MyApp()
   a2 = MyApp(p)   # pass p object into a2
   a1.doIt(self)   # prints default value = 0
   a2.doIt(self)   # prints 42, the value of p

test()

When I run it, I get an error:
Traceback (most recent call last):
   File "alan_class.py", line 22, in
 test()
   File "alan_class.py", line 19, in test
 a1.doIt(self)   # prints default value = 0
NameError: global name 'self' is not defined



Very good.  Now I can see the problem(s).

The immediate problem is that inside function test() (which is an 
ordinary function, not a method), you refer to self, which is not 
defined there.  just omit it there (both times), since the "self" value 
will automatically be supplied when you use the a1.doIt() syntax.


Which takes me to the next problem.  You've defined a method inside 
class MyApp, without supplying the self parameter.  All methods of a 
class will have a 'self' parameter as the first argument (until you get 
to classmethod and staticmethod, which come much later in your 
learning).  You have it everywhere else, but not here.


This is admittedly a confusing aspect of class programming in Python. 
When you call a method, you do not supply an explicit self, it's implied 
by the object you used to get that method.  And in the method, you do 
have to declare an explicit self as the first parameter.  Thus when you 
get error messages, the compiler's concept of how many arguments there 
are, and how many there should be, is confusing.


Trust me when I say there are reasons why this should be, having to do 
with passing bound methods around.  But at this point of your learning, 
it's a confusion with no obvious benefit.


Without trying it, here's what I think those two portions of coee should 
look like:


class MyApp:
   .
  def doIt(self):
  self.obj.sayIt() # use object

def test():
  p = Printer(42)
  a1 = MyApp()
  a2 = MyApp(p)   # pass p object into a2
  a1.doIt()   # prints default value = 0
  a2.doIt()   # prints 42, the value of p



--

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


Re: [Tutor] exercise with classes

2012-02-09 Thread Tonu Mikk
On Mon, Feb 6, 2012 at 12:58 PM, Dave Angel  wrote:

> On 02/06/2012 01:24 PM, Tonu Mikk wrote:
>
>> Now I get an error:  NameError: global name 'self' is not define.
>>
>> Tonu
>>
>>
>>  Put your remarks after the stuff you quote.  You're top-posting, which
> makes the reply difficult to follow.
>
> Use copy/paste to describe an error message.  You retyped the one above,
> and added a typo.  Include the whole error, which includes the stack trace.
>
> If you had followed Nate's advice, you couldn't have gotten that error at
> all, so you'll also need to post the code that actually triggers the error.


Let's try this one more time.  I thought that I would try to make Alan's
code work.  Here is the version what I have now:

class Printer:
  def __init__(self,number=0):
 self.value = number
  def sayIt(self):
 print self.value

class MyApp:
  def __init__(self, aPrinter = None):
  if aPrinter == None: # if no object passed create one
 aPrinter = Printer()
  self.obj = aPrinter  # assign object
  def doIt():
  self.obj.sayIt() # use object

def test():
  p = Printer(42)
  a1 = MyApp()
  a2 = MyApp(p)   # pass p object into a2
  a1.doIt(self)   # prints default value = 0
  a2.doIt(self)   # prints 42, the value of p

test()

When I run it, I get an error:
Traceback (most recent call last):
  File "alan_class.py", line 22, in 
test()
  File "alan_class.py", line 19, in test
a1.doIt(self)   # prints default value = 0
NameError: global name 'self' is not defined



>
>
>
>
>
>
> --
>
> DaveA
>
>


-- 
Tonu Mikk
Disability Services, Office for Equity and Diversity
612 625-3307
tm...@umn.edu
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes

2012-02-06 Thread Dave Angel

On 02/06/2012 01:24 PM, Tonu Mikk wrote:

Now I get an error:  NameError: global name 'self' is not define.

Tonu


Put your remarks after the stuff you quote.  You're top-posting, which 
makes the reply difficult to follow.


Use copy/paste to describe an error message.  You retyped the one above, 
and added a typo.  Include the whole error, which includes the stack trace.


If you had followed Nate's advice, you couldn't have gotten that error 
at all, so you'll also need to post the code that actually triggers the 
error.






--

DaveA

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


Re: [Tutor] exercise with classes

2012-02-06 Thread Tonu Mikk
Now I get an error:  NameError: global name 'self' is not define.

Tonu

On Mon, Feb 6, 2012 at 10:19 AM, Nate Lastname  wrote:

> Hey Tonu,
>
> The problem is that in your statement definition, you are not
> including the self argument.  Your definition needs to be something
> like:
> def dolt(self):
>   # Do stuff.
> For more info on the self keyword, see
> http://docs.python.org/tutorial/classes.html, section 9.3.2.
>
> On 2/6/12, Tonu Mikk  wrote:
> > Alan, thanks for explaining about passing objects to classes.  This is an
> > important concept for me to understand.
> >
> > I tried running the code, but run into an error that I could not resolve:
> >
> > TypeError: doIt() takes no arguments (1 given).
> >
> > Tonu
> >
> > On Thu, Feb 2, 2012 at 7:09 PM, Alan Gauld  >wrote:
> >
> >> On 02/02/12 17:36, Tonu Mikk wrote:
> >>
> >>  So far I have searched for info on how to pass variables from one class
> >>> to another and have been able to create a small two class program
> >>> (attached).   But I seem unable to generalize from here and apply this
> >>> to the game exercise.  What would you suggest for me to try next?
> >>>
> >>
> >> Remember that OOP is about creating objects from classes.
> >> You can pass an object to another rather than just the
> >> variables, in fact its preferable!
> >>
> >> Also remember that you can create many objects from one class.
> >> So just because you have one Room class doesn't mean you are
> >> stuck with one room object. You can have many and each can
> >> be connected to another.
> >>
> >> You can get rooms to describe themselves, you can enter a room.
> >> You might even be able to create new rooms or destroy existing ones.
> These
> >> actions can all be methods of your Room class.
> >>
> >> Here is an example somewhat like yours that passes objects:
> >>
> >> class Printer:
> >>   def __init__(self,number=0):
> >>  self.value = number
> >>   def sayIt(self):
> >>  print self.value
> >>
> >> class MyApp:
> >>   def __init__(self, aPrinter = None):
> >>   if aPrinter == None: # if no object passed create one
> >>  aPrinter = Printer()
> >>   self.obj = aPrinter  # assign object
> >>   def doIt()
> >>   self.obj.sayIt() # use object
> >>
> >> def test()
> >>   p = Printer(42)
> >>   a1  MyApp()
> >>   a2 = MyApp(p)   # pass p object into a2
> >>   a1.doIt()   # prints default value = 0
> >>   a2.doIt()   # prints 42, the value of p
> >>
> >> test()
> >>
> >> HTH,
> >>
> >> --
> >> Alan G
> >> 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<
> http://mail.python.org/mailman/listinfo/tutor>
> >>
> >
> >
> >
> > --
> > Tonu Mikk
> > Disability Services, Office for Equity and Diversity
> > 612 625-3307
> > tm...@umn.edu
> >
>
>
> --
> My Blog - Defenestration Coding
>
> http://defenestrationcoding.wordpress.com/
>



-- 
Tonu Mikk
Disability Services, Office for Equity and Diversity
612 625-3307
tm...@umn.edu
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes

2012-02-06 Thread Alan Gauld

On 06/02/12 16:12, Tonu Mikk wrote:

Alan, thanks for explaining about passing objects to classes.  This is
an important concept for me to understand.

I tried running the code, but run into an error that I could not resolve:

TypeError: doIt() takes no arguments (1 given).


Sorry the code was only meant for illustration and had not nbeen tested.
I forgot the self in the definition of doit()... And probably other 
stuff too!


Alan G.

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


Re: [Tutor] exercise with classes

2012-02-06 Thread Nate Lastname
Hey Tonu,

The problem is that in your statement definition, you are not
including the self argument.  Your definition needs to be something
like:
def dolt(self):
   # Do stuff.
For more info on the self keyword, see
http://docs.python.org/tutorial/classes.html, section 9.3.2.

On 2/6/12, Tonu Mikk  wrote:
> Alan, thanks for explaining about passing objects to classes.  This is an
> important concept for me to understand.
>
> I tried running the code, but run into an error that I could not resolve:
>
> TypeError: doIt() takes no arguments (1 given).
>
> Tonu
>
> On Thu, Feb 2, 2012 at 7:09 PM, Alan Gauld wrote:
>
>> On 02/02/12 17:36, Tonu Mikk wrote:
>>
>>  So far I have searched for info on how to pass variables from one class
>>> to another and have been able to create a small two class program
>>> (attached).   But I seem unable to generalize from here and apply this
>>> to the game exercise.  What would you suggest for me to try next?
>>>
>>
>> Remember that OOP is about creating objects from classes.
>> You can pass an object to another rather than just the
>> variables, in fact its preferable!
>>
>> Also remember that you can create many objects from one class.
>> So just because you have one Room class doesn't mean you are
>> stuck with one room object. You can have many and each can
>> be connected to another.
>>
>> You can get rooms to describe themselves, you can enter a room.
>> You might even be able to create new rooms or destroy existing ones. These
>> actions can all be methods of your Room class.
>>
>> Here is an example somewhat like yours that passes objects:
>>
>> class Printer:
>>   def __init__(self,number=0):
>>  self.value = number
>>   def sayIt(self):
>>  print self.value
>>
>> class MyApp:
>>   def __init__(self, aPrinter = None):
>>   if aPrinter == None: # if no object passed create one
>>  aPrinter = Printer()
>>   self.obj = aPrinter  # assign object
>>   def doIt()
>>   self.obj.sayIt() # use object
>>
>> def test()
>>   p = Printer(42)
>>   a1  MyApp()
>>   a2 = MyApp(p)   # pass p object into a2
>>   a1.doIt()   # prints default value = 0
>>   a2.doIt()   # prints 42, the value of p
>>
>> test()
>>
>> HTH,
>>
>> --
>> Alan G
>> 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
>>
>
>
>
> --
> Tonu Mikk
> Disability Services, Office for Equity and Diversity
> 612 625-3307
> tm...@umn.edu
>


-- 
My Blog - Defenestration Coding

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


Re: [Tutor] exercise with classes

2012-02-06 Thread Tonu Mikk
Alan, thanks for explaining about passing objects to classes.  This is an
important concept for me to understand.

I tried running the code, but run into an error that I could not resolve:

TypeError: doIt() takes no arguments (1 given).

Tonu

On Thu, Feb 2, 2012 at 7:09 PM, Alan Gauld wrote:

> On 02/02/12 17:36, Tonu Mikk wrote:
>
>  So far I have searched for info on how to pass variables from one class
>> to another and have been able to create a small two class program
>> (attached).   But I seem unable to generalize from here and apply this
>> to the game exercise.  What would you suggest for me to try next?
>>
>
> Remember that OOP is about creating objects from classes.
> You can pass an object to another rather than just the
> variables, in fact its preferable!
>
> Also remember that you can create many objects from one class.
> So just because you have one Room class doesn't mean you are
> stuck with one room object. You can have many and each can
> be connected to another.
>
> You can get rooms to describe themselves, you can enter a room.
> You might even be able to create new rooms or destroy existing ones. These
> actions can all be methods of your Room class.
>
> Here is an example somewhat like yours that passes objects:
>
> class Printer:
>   def __init__(self,number=0):
>  self.value = number
>   def sayIt(self):
>  print self.value
>
> class MyApp:
>   def __init__(self, aPrinter = None):
>   if aPrinter == None: # if no object passed create one
>  aPrinter = Printer()
>   self.obj = aPrinter  # assign object
>   def doIt()
>   self.obj.sayIt() # use object
>
> def test()
>   p = Printer(42)
>   a1  MyApp()
>   a2 = MyApp(p)   # pass p object into a2
>   a1.doIt()   # prints default value = 0
>   a2.doIt()   # prints 42, the value of p
>
> test()
>
> HTH,
>
> --
> Alan G
> 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
>



-- 
Tonu Mikk
Disability Services, Office for Equity and Diversity
612 625-3307
tm...@umn.edu
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes

2012-02-02 Thread Alan Gauld

On 02/02/12 17:36, Tonu Mikk wrote:


So far I have searched for info on how to pass variables from one class
to another and have been able to create a small two class program
(attached).   But I seem unable to generalize from here and apply this
to the game exercise.  What would you suggest for me to try next?


Remember that OOP is about creating objects from classes.
You can pass an object to another rather than just the
variables, in fact its preferable!

Also remember that you can create many objects from one class.
So just because you have one Room class doesn't mean you are
stuck with one room object. You can have many and each can
be connected to another.

You can get rooms to describe themselves, you can enter a room.
You might even be able to create new rooms or destroy existing ones. 
These actions can all be methods of your Room class.


Here is an example somewhat like yours that passes objects:

class Printer:
   def __init__(self,number=0):
  self.value = number
   def sayIt(self):
  print self.value

class MyApp:
   def __init__(self, aPrinter = None):
   if aPrinter == None: # if no object passed create one
  aPrinter = Printer()
   self.obj = aPrinter  # assign object
   def doIt()
   self.obj.sayIt() # use object

def test()
   p = Printer(42)
   a1  MyApp()
   a2 = MyApp(p)   # pass p object into a2
   a1.doIt()   # prints default value = 0
   a2.doIt()   # prints 42, the value of p

test()

HTH,

--
Alan G
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] exercise with classes

2012-02-02 Thread Alexander Etter
On Feb 2, 2012, at 12:36, Tonu Mikk  wrote:

>  I feel stumped and don't know how to go about creating this game with two 
> classes.
> 
> So far I have searched for info on how to pass variables from one class to 
> another and have been able to create a small two class program (attached). 

> Thank you,
> Tonu 
> 
> __
Hi Tonu. 
I'm fairly certain that your second class is missing the most important 
function of a class, the __init__ function! It's necessary to initialize the 
object. Add it to your second class and see how it changes things. 
Alexander___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise with classes

2012-02-02 Thread Prasad, Ramit
>I am learning Python using the "Learn Python the Hard Way" book by Zed Shaw.  
>I reached exercise 42 where we learn about Python classes.  The exercise shows 
>a game with one class that includes all the definitions for playing the game.  
>For extra credit we are asked to create another version of this game where we 
>use two classes - one for the room definitions and the other for the playing 
>the game.  I feel stumped and don't know how to go about creating this game 
>with two classes.
> So far I have searched for info on how to pass variables from one class to 
> another and have been able to create a small two class program (attached).   
> But I seem unable to generalize from here and apply this to the game 
> exercise.  What would you suggest for me to try next?  

It is usually better to include short code directly in your email 
or link to something like pastebin for longer samples. Many of the people
on this list do access it from email and they do not get attachments.

As for the extra credit: You can store the possible actions 
in the room (dodge, shoot, etc.). So for central_corridor (an instance
of class Room) you have the player do something like 

action = raw_input('>)
consequence = central_corridor.do_action( action )
new_room = Game.getRoom( consequence ) 
# Might need to replace Game with self depending on where this code is located.
action = raw_input('>)
consequence = new_room.do_action( action )
...


Ramit


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

--

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] exercise with classes

2012-02-02 Thread Tonu Mikk
I am learning Python using the "Learn Python the Hard Way" book by Zed
Shaw.
 I reached exercise 42 where we learn about Python
classes.
 The exercise shows a game with one class that includes all the definitions
for playing the game.  For extra credit we are asked to create another
version of this game where we use two classes - one for the room
definitions and the other for the playing the game.  I feel stumped and
don't know how to go about creating this game with two classes.

So far I have searched for info on how to pass variables from one class to
another and have been able to create a small two class program (attached).
  But I seem unable to generalize from here and apply this to the game
exercise.  What would you suggest for me to try next?

Thank you,

Tonu


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


Re: [Tutor] Exercise suggestions

2011-01-22 Thread Steven D'Aprano

Elwin Estle wrote:


I seem to remember that Python (is it supposed to be capitalized?  Official 
logo is in lower case)


The official logo of Apple doesn't even have an "A" in it, but we don't 
spell the company "pple" :)


By long-standing convention and practice, the programming language is 
spelled with an initial capital ("Python"), and the executable command 
on computers is spelled in lowercase ("python").


Go to http://www.python.org/ and you will see the language is always 
referred to as Python rather than python.




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


Re: [Tutor] Exercise suggestions

2011-01-22 Thread Alan Gauld


"michael scott"  wrote

don't need a person walking me through it, a simple, "build a 
program that asks
a user to give you a name and create permutations of it" is great. 
Of course
that example is elementary, but that is the gist of the responses 
I'm looking

for.


So have you done it yet?
And then added a GUI?
Or generated a group of random letters and let the user guess the
longest word they can make up using those letters - and compare
the result that the computer found based on a dictionary of  valid
words?

Then add a second user so it becomes a competition with the
computer validating the two results and declaring a winner.

I just have no idea of what kind of programs to build, my ignorance 
is

holding me back in my opinion.


Your suggestion was a good start then use imagination on how to
improve it. Then go looking for things you do manually on your
computer (or in a notebook) and automate them. You need experience
and the more relevant and real your programmes are the better.

The one problem with Opensource projects compasred to
professional/commercial coding is that there is nearly always
a dearth of design documentation. Moost commercial software
shops will produce architecure and design documentation that
helps you find your way aropund a project. There are probably
exceptions (Linux is one which has several books written about
the design) but thats my experience of Opensource code. You
just have to wade in and start reading.

Playing with a debugger and grepping for likely strings in functions
is a good starting point - and asking questions on the devel mailing
lists too.


Any response is welcomed, but I do ask if you are critical of me


Your aspirations are good but remember that most professional
programmers are college or universitry trained. Its not an easy
job to market to crack without professional qualifications. You
need a lot of experience to evidence your competence.

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] Exercise suggestions

2011-01-22 Thread wesley chun
> [...] I'm finding that I
> understand concepts, but have no real way to implement them.
>
> So now my problem emerges... can anyone give me suggestions of exercises I
> should do to help improve  my knowledge of what I can "actually" do with the
> concepts I have.


michael,

first of all, welcome to Python! a couple of suggestions...

1) the best thing to do is to "scratch an itch" now that you know how
to code. what is an app that would really benefit you on a daily (or
weekly) basis? it can be anything. for me, i've created small apps to:
a) validate the syntax of a Python script, b) create PowerPoint files
on the fly based on a plain text file (because i didn't want to edit
slides in PowerPoint anymore), etc.

2) if you happen to have my book, i put in numerous exercises at the
end of every chapter to complement the reading and to enforce that you
did pick up on the material within that and previous chapters. i've
gotten good feedback from readers who say that they were really useful
in hammering home the concepts.

good luck!
-- wesley
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"Core Python Programming", Prentice Hall, (c)2007,2001
"Python Fundamentals", Prentice Hall, (c)2009
    http://corepython.com

wesley.chun : wescpy-gmail.com : @wescpy
python training and technical consulting
cyberweb.consulting : silicon valley, ca
http://cyberwebconsulting.com
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise suggestions

2011-01-22 Thread Elwin Estle
Okay, here's another one.  I started this geez...30 years ago?  On a Texas 
Instruments 99/4A computer.  I've never gotten around to doing anything further 
with the idea.

It was a game called "Piledriver".  The idea was that in a distant future, 
energy demands had become so great that there were all these competing energy 
companies running nuclear reactors to generate power.  The reactors were run by 
human brains hardwired into the control systems, i.e. "Atomic Pile", or 
"Piledrivers"  (hey, I was 19 when I thought of this, okay?)

The player is, of course, one of these "piledrivers".  The idea was that they 
started with a basic nuclear generator set up, providing power to a certain 
area of a city, at a certain cost.  They had to take the profits from their 
efforts and build an "energy empire".  They had to deal with things like 
installing and maintaining transmission lines, additional power generating 
capability, etc.  They had to fund day to day expenses, pay for security 
forces, which served several purposes, to keep any "unauthorized persons" from 
stealing their fuel either for competitors or perhaps for terrorists wanting to 
build bombs, etc., or environmentalists wanting to shut down your evil, earth 
threatening facility... The security forces also act to prevent any other 
industrial espionage from competitors, terrorists, or over zealous 
environmental groups.

Also funded were your own espionage agents to do the same to competitors, 
infiltrate militant environmental groups, get blackmail material on politicians 
that could help/hurt your business, etc  There would be  R & D specialists who 
would periodically introduce new technologies to increase the efficiency or 
safety of the nuclear generators. (I had this sort of random name generator I 
made for the scientists creations, that would spit out things like "Your R & D 
department have created the hypertronic whatchamacallit, which increases your 
reactor efficiency by .02%", or somesuch.

You had to control the rate you charged customers for your energy.  Too much 
and they might switch to a competitor.  Too little and you couldn't afford to 
pay your security guys, PR department (advertising...or there to put a "spin" 
on things in case there was some sort of industrial "incident" at your 
facility), or R&D department.  You paid for advertising, public service stuff 
and other PR to keep the environmentalists of your back.

You could also do things like fund lobbyists or bribe politicians to get 
preferential treatment from the government.

Bad things could happen, like faulty equipment creating an "incident", or 
perhaps a competitor manages to infiltrate a mole into your R&D, PR, or 
Security forces.  You might end up with a horde of angry environmentalists at 
your gates, complete with TV crews, etc, and have to deal with that (causing 
energy subscribers to leave for competitors, or government fines, etc.)

My original idea also allowed for some hands on stuff.  There would be these 
little maintenance bots that you'd "drive" around the facility, looking for 
things like faulty equipment, or sabotage by competitors or terrorists...or 
even environmentalists.

...anyway, you get the idea.  Perhaps a lame idea in the context of today's 3D 
graphical games,  but if properly implemented...maybe it would be fun.

--- On Sat, 1/22/11, michael scott  wrote:

From: michael scott 
Subject: [Tutor] Exercise suggestions
To: tutor@python.org
Date: Saturday, January 22, 2011, 11:56 AM

I am new to programming, I intend to get an entry level job programming next 
year (or a little bit longer). I am switching fields and not going to college, 
but kinda "self teaching" myself through various methods. I currently 
understand concepts in programming in python up to classes (like I understand 
how to make objects, I understand inheritance, etc...), and I have experimented 
with building guis in Tkinter,  but I'm finding that I understand concepts, but 
have no real way to implement them.

So now my problem emerges... can anyone give me suggestions of exercises I 
should do to help improve  my knowledge of what I can "actually" do with the 
concepts I have.

My main goal is to get to the point where I can assist in fixing bugs in open 
source programs (I'll be learning
 C++ in a couple months as well), but when I look at bugs / source code of 
larger programs, I am just so blown away at how little I understand. So I need 
to find a way to bridge the gap between my current level and the level needed 
to contribute to open source programs. A lofty goal I understand, but it is my 
goal. And I am very dedicated to reaching it.

Anyways, given my situation, do you good people have any suggestions for me. I 
don't need a person walking me through it, a simple, "

Re: [Tutor] Exercise suggestions

2011-01-22 Thread ian douglas
For me, the quickest way to jump into a programming language was seeing 
a current open source project and say "I wish it did this too, or that 
instead ..." and implement it myself. Not only do you learn about the 
project itself, and pick up some tips and tricks along the way, you then 
also have a way to tell the original author about something you could 
contribute if others are interested.


So, it's not just working on something to make it "better", you do it to 
make it "personal" to your own needs instead.



On 01/22/2011 01:52 PM, Elwin Estle wrote:
Sure, there are tons of these sorts of apps already, but so what?  
Maybe you can make one that is better.



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


Re: [Tutor] Exercise suggestions

2011-01-22 Thread Elwin Estle
Internet Explorer, and they can print them off.  No more digging 
for the original master copy in a three ring binder.  No more auditing to make 
sure that the master in the binder is up to date, since the pdf is made 
directly from the original Excel master...which is my boss's responsibility ;-).

Phase two:  Still partially vaporware.  I export the original Excel file to 
.csv format.  I have a Tcl based parser  (hey, I am only just switching to 
python here, okay?) that loads one of these files, and parses it into XML 
format.  The idea is to take these XML files and use them in some sort of web 
app to make an HTML form based version that the operators can just fill out.  
These will be searchable and otherwise data-massageable...right now, the 
operators fill 'em out and we file them until there is a problem.  The 
engineers would also be able access the info to see if there are any trends 
with features on parts that are going out of spec, part runs vs. tool wear, etc.

Here's another idea.  Imagine that you are the owner of some sort of 
restaurant.  Write yourself a Point of Sale app for your restaurant.  I've 
always thought it would be neat to walk into say, a pizza place and use some 
sort of kiosk with drag and drop virtual pizza ingredients.  You design your 
pizza, it sends the order to the kitchen, you pay, and then go sit down until 
someone brings your order.    This could work for a fast food place, or 
whatever.

Further restaurant idea, more complicated.  I had this one years ago, but the 
computer hardware back then wasn't up to it...at least the hardware I could 
afford, anyway.  This is a simulation game of sorts.   The user designs a 
restaurant from the ground up, chooses the physical layout of the dining room, 
kitchen, etc.  Designs a menu.  Designates where the food for creating that 
menu is stored.  Hires servers, a chef, prep cooks, etc., then fires up the 
virtual restaurant.  From here they get to see how the layout of their kitchen 
and dining room works.  Are people getting in each other's way in the kitchen 
or the dining room?  Is your kitchen able to keep up?  Do you have enough 
staff?  Are the dishes being washed fast enough?  Are you buying enough 
ingredients to make sure that you don't run out of food?  Or have too much 
stock on hand and you are losing some to spoilage?  Are you managing costs and 
making a profit?   What about
 unexpected events, like equipment breakdowns, or a sudden influx of customers 
on a night you expected things to be a bit slow?  What if you get a surprise 
visit from the health inspector?  What if a customer gets food poisoning and 
sues you?  What if an employee is injured at work?  (slips, falls, cuts, etc.)  
What if a rival restaurant offers your chef more money?

(and just thinking about this, the whole idea above could also be applied to a 
factory, which is all a restaurant is, really, a factory that produces edible 
consumables, rather than durable goods).

What about a facebook app?  I think you can do those in python.

There are all sorts of scenarios in the above for one to analyze and create 
code to simulate, etc.

Enough of my blathering.   Basicallyfind something, a problem, an imaginary 
idea like a game, and then figure out how to apply programming solutions to 
that problem.

--- On Sat, 1/22/11, michael scott  wrote:

From: michael scott 
Subject: [Tutor] Exercise suggestions
To: tutor@python.org
Date: Saturday, January 22, 2011, 11:56 AM

I am new to programming, I intend to get an entry level job programming next 
year (or a little bit longer). I am switching fields and not going to college, 
but kinda "self teaching" myself through various methods. I currently 
understand concepts in programming in python up to classes (like I understand 
how to make objects, I understand inheritance, etc...), and I have experimented 
with building guis in Tkinter,  but I'm finding that I understand concepts, but 
have no real way to implement them.

So now my problem emerges... can anyone give me suggestions of exercises I 
should do to help improve  my knowledge of what I can "actually" do with the 
concepts I have.

My main goal is to get to the point where I can assist in fixing bugs in open 
source programs (I'll be learning
 C++ in a couple months as well), but when I look at bugs / source code of 
larger programs, I am just so blown away at how little I understand. So I need 
to find a way to bridge the gap between my current level and the level needed 
to contribute to open source programs. A lofty goal I understand, but it is my 
goal. And I am very dedicated to reaching it.

Anyways, given my situation, do you good people have any suggestions for me. I 
don't need a person walking me through it, a simple, "build a program that asks 
a user to give you a name and create permutations of it" is great. Of course 
that example is elementary, but th

Re: [Tutor] Exercise suggestions

2011-01-22 Thread Corey Richardson

On 01/22/2011 03:09 PM, David Hutto wrote:

When I felt I was ready to start doing some work, I got involved in an open
source project. It's definitely an experience! Try going through
http://freshmeat.net/


Isn't that for those that aren't given real apprenticeship?:)



Programming is my hobby, not my profession. Also given the fact that 
I've yet to graduate high school, OS was the way to go for me.


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


Re: [Tutor] Exercise suggestions

2011-01-22 Thread R. Alan Monroe
> I just have no idea of what kind of programs to build,

Some of the stock answers for this question are:
http://projecteuler.net/
http://www.pythonchallenge.com/

I usually suggest porting one of the old games in:
http://www.atariarchives.org/basicgames/

Alan

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


Re: [Tutor] Exercise suggestions

2011-01-22 Thread David Hutto
On Sat, Jan 22, 2011 at 3:02 PM, Corey Richardson  wrote:
> On 01/22/2011 11:56 AM, michael scott wrote:
>>
>> I am new to programming, I intend to get an entry level job programming
>> next year (or a little bit longer). I am switching fields and not going
>> to college, but kinda "self teaching" myself through various methods. I
>> currently understand concepts in programming in python up to classes
>> (like I understand how to make objects, I understand inheritance,
>> etc...), and I have experimented with building guis in Tkinter, but I'm
>> finding that I understand concepts, but have no real way to implement
>> them.
>>
>> So now my problem emerges... can anyone give me suggestions of exercises
>> I should do to help improve my knowledge of what I can "actually" do
>> with the concepts I have.
>>
>> My main goal is to get to the point where I can assist in fixing bugs in
>> open source programs (I'll be learning C++ in a couple months as well),
>> but when I look at bugs / source code of larger programs, I am just so
>> blown away at how little I understand. So I need to find a way to bridge
>> the gap between my current level and the level needed to contribute to
>> open source programs. A lofty goal I understand, but it is my goal. And
>> I am very dedicated to reaching it.
>>
>> Anyways, given my situation, do you good people have any suggestions for
>> me. I don't need a person walking me through it, a simple, "build a
>> program that asks a user to give you a name and create permutations of
>> it" is great. Of course that example is elementary, but that is the gist
>> of the responses I'm looking for. I just have no idea of what kind of
>> programs to build, my ignorance is holding me back in my opinion.
>>
>> Any response is welcomed, but I do ask if you are critical of me, please
>> offer a method for me to improve the deficiency / deficiencies I have.
>> Thank you so much for reading my inquiry :)
>
> When I felt I was ready to start doing some work, I got involved in an open
> source project. It's definitely an experience! Try going through
> http://freshmeat.net/

Isn't that for those that aren't given real apprenticeship?:)

and remake a few things. Can't really recommend
> anything else, fresh out of ideas too ;-)

Remix IS evolution. So ideas are really irrelevant.

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



-- 
The lawyer in me says argue...even if you're wrong. The scientist in
me... says shut up, listen, and then argue. But the lawyer won on
appeal, so now I have to argue due to a court order.

Furthermore, if you could be a scientific celebrity, would you want
einstein sitting around with you on saturday morning, while you're
sitting in your undies, watching Underdog?...Or better yet, would
Einstein want you to violate his Underdog time?

Can you imagine Einstein sitting around in his underware? Thinking
about the relativity between his pubic nardsac, and his Fruit of the
Looms, while knocking a few Dorito's crumbs off his inner brilliant
white thighs, and hailing E = mc**2, and licking the orangy,
delicious, Doritoey crust that layered his genetically rippled
fingertips?

But then again, J. Edgar Hoover would want his pantyhose intertwined
within the equation.

However, I digress, momentarily.

But Einstein gave freely, for humanity, not for gain, other than
personal freedom.

An equation that benefited all, and yet gain is a personal product.

Also, if you can answer it, is gravity anymore than interplanetary static cling?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise suggestions

2011-01-22 Thread Corey Richardson

On 01/22/2011 11:56 AM, michael scott wrote:

I am new to programming, I intend to get an entry level job programming
next year (or a little bit longer). I am switching fields and not going
to college, but kinda "self teaching" myself through various methods. I
currently understand concepts in programming in python up to classes
(like I understand how to make objects, I understand inheritance,
etc...), and I have experimented with building guis in Tkinter, but I'm
finding that I understand concepts, but have no real way to implement them.

So now my problem emerges... can anyone give me suggestions of exercises
I should do to help improve my knowledge of what I can "actually" do
with the concepts I have.

My main goal is to get to the point where I can assist in fixing bugs in
open source programs (I'll be learning C++ in a couple months as well),
but when I look at bugs / source code of larger programs, I am just so
blown away at how little I understand. So I need to find a way to bridge
the gap between my current level and the level needed to contribute to
open source programs. A lofty goal I understand, but it is my goal. And
I am very dedicated to reaching it.

Anyways, given my situation, do you good people have any suggestions for
me. I don't need a person walking me through it, a simple, "build a
program that asks a user to give you a name and create permutations of
it" is great. Of course that example is elementary, but that is the gist
of the responses I'm looking for. I just have no idea of what kind of
programs to build, my ignorance is holding me back in my opinion.

Any response is welcomed, but I do ask if you are critical of me, please
offer a method for me to improve the deficiency / deficiencies I have.
Thank you so much for reading my inquiry :)


When I felt I was ready to start doing some work, I got involved in an 
open source project. It's definitely an experience! Try going through 
http://freshmeat.net/ and remake a few things. Can't really recommend 
anything else, fresh out of ideas too ;-)


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


[Tutor] Exercise suggestions

2011-01-22 Thread michael scott
I am new to programming, I intend to get an entry level job programming next 
year (or a little bit longer). I am switching fields and not going to college, 
but kinda "self teaching" myself through various methods. I currently 
understand 
concepts in programming in python up to classes (like I understand how to make 
objects, I understand inheritance, etc...), and I have experimented with 
building guis in Tkinter,  but I'm finding that I understand concepts, but have 
no real way to implement them.

So now my problem emerges... can anyone give me suggestions of exercises I 
should do to help improve  my knowledge of what I can "actually" do with the 
concepts I have.

My main goal is to get to the point where I can assist in fixing bugs in open 
source programs (I'll be learning C++ in a couple months as well), but when I 
look at bugs / source code of larger programs, I am just so blown away at how 
little I understand. So I need to find a way to bridge the gap between my 
current level and the level needed to contribute to open source programs. A 
lofty goal I understand, but it is my goal. And I am very dedicated to reaching 
it.

Anyways, given my situation, do you good people have any suggestions for me. I 
don't need a person walking me through it, a simple, "build a program that asks 
a user to give you a name and create permutations of it" is great. Of course 
that example is elementary, but that is the gist of the responses I'm looking 
for. I just have no idea of what kind of programs to build, my ignorance is 
holding me back in my opinion.

Any response is welcomed, but I do ask if you are critical of me, please offer 
a 
method for me to improve the deficiency / deficiencies  I have. Thank you so 
much for reading my inquiry :)

 What is it about you... that intrigues me so?



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


Re: [Tutor] exercise correct ??

2010-09-07 Thread Roelof Wobben


 


Date: Tue, 7 Sep 2010 00:52:38 -0700
From: alan.ga...@btinternet.com
Subject: Re: [Tutor] exercise correct ??
To: rwob...@hotmail.com; tutor@python.org









Oke, the 4 is a starting point for the index.
 
Next problem.
 
The begin looks like this :
 
 index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)

But in the module I get this result :
 
val = 5
seq = (1, 2, 4, 5, 6, 10, 5, 5
 
So the 4 is not avaible anymore.


Yes it is. It is the start parameter.

The function definition is

def index_of(val, seq, start=0):

val is the first value, 5, seq is the tuple and start is 4.
 



Now I can change the header to  index(val, seq, start=0) to index (val, seq, 
start)
But I think that's not what the exercise wants.


Why would you want to do that? It would force you to provide a start value 
for every call. The point of having a default value (=0) is so that you do not 
need to specify start every time you use the function. But eveb if you do not
use the start value it will still have a value, 0.
There is no difference, you can access it exactly like the other parameters.
Just use its name.
 
HTH,

Alan G.
 
Oke, 
 
Then this is the solution :
 
def index_of(val, seq, start=0):
"""
  >>> index_of(9, [1, 7, 11, 9, 10])
  3
  >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5))
  3
  >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
  6
  >>> index_of('y', 'happy birthday')
  4
  >>> index_of('banana', ['apple', 'banana', 'cherry', 'date'])
  1
  >>> index_of(5, [2, 3, 4])
  -1
  >>> index_of('b', ['apple', 'banana', 'cherry', 'date'])
  -1
"""
try:
plek = seq.index(val, start)
except:
plek = -1
return plek 
 
Roelof


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


Re: [Tutor] exercise correct ??

2010-09-07 Thread ALAN GAULD


Oke, the 4 is a starting point for the index.
> 
>Next problem.
> 
>The begin looks like this :
> 
> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
>
>But in the module I get this result :
> 
>val = 5
>seq = (1, 2, 4, 5, 6, 10, 5, 5
> 
>So the 4 is not avaible anymore.
>
Yes it is. It is the start parameter.

The function definition is

def index_of(val, seq, start=0):

val is the first value, 5, seq is the tuple and start is 4.
 

Now I can change the header to  index(val, seq, start=0) to index (val, seq, 
start)
>But I think that's not what the exercise wants.
>
Why would you want to do that? It would force you to provide a start value 
for every call. The point of having a default value (=0) is so that you do not 
need to specify start every time you use the function. But eveb if you do not
use the start value it will still have a value, 0.
There is no difference, you can access it exactly like the other parameters.
Just use its name.
 
HTH,

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


Re: [Tutor] exercise correct ??

2010-09-07 Thread Roelof Wobben

Hello, 

 

Oke, the 4 is a starting point for the index.

 

Next problem.

 

The begin looks like this :

 

 index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)


But in the module I get this result :

 

val = 5

seq = (1, 2, 4, 5, 6, 10, 5, 5

 

So the 4 is not avaible anymore.

 

Now I can change the header to  index(val, seq, start=0) to index (val, seq, 
start)

But I think that's not what the exercise wants.

 

Is there another way I can use the 4 ?

 

Roelof

 


 
> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Mon, 6 Sep 2010 23:28:22 +0100
> Subject: Re: [Tutor] exercise correct ??
> 
> 
> "Roelof Wobben"  wrote
> 
> #
> def index_of(val, seq, start=0):
> """
> >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
> 6
> """
> 
> But I get this message :
> Failed example:
> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
> Expected:
> 6
> Got:
> 3
> #
> 
> > But in that tuple 5 is on position 3.
> > Is the exercise here wrong ?
> 
> No because the start position is 4 so you don;t see the 5 in position 
> 3.
> 
> 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
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise correct ??

2010-09-06 Thread Alan Gauld


"Roelof Wobben"  wrote

#
def index_of(val, seq, start=0):
   """
 >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
 6
   """

But I get this message :
Failed example:
index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
Expected:
6
Got:
3
#


But in that tuple 5 is on position 3.
Is the exercise here wrong ?


No because the start position is 4 so you don;t see the 5 in position 
3.


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] exercise correct ??

2010-09-06 Thread Sander Sweers
On 6 September 2010 21:45, Sander Sweers  wrote:
>> Is the exercise here wrong ?
>
> Looks like it, or it's a typo.

Now that I had a better look the test is correct. Now it is up to you
to figure out why your index_of() fails. Walter gave you a good hint.

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


Re: [Tutor] exercise correct ??

2010-09-06 Thread Sander Sweers
On 6 September 2010 22:28, Roelof Wobben  wrote:
> As far as I know index is not a part of tuple so I have to convert it to a
> list so I can use index.

As of version 2.6/3 a tuple does have index(). Not sure which version
you are using.

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


Re: [Tutor] exercise correct ??

2010-09-06 Thread Roelof Wobben


 

> Date: Mon, 6 Sep 2010 21:45:17 +0200
> Subject: Re: [Tutor] exercise correct ??
> From: sander.swe...@gmail.com
> To: rwob...@hotmail.com
> CC: tutor@python.org
> 
> On 6 September 2010 19:32, Roelof Wobben  wrote:
> > def index_of(val, seq, start=0):
> > """
> >   >>> index_of(9, [1, 7, 11, 9, 10])
> >   3
> >   >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5))
> >   3
> >   >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
> >   6
> >   >>> index_of('y', 'happy birthday')
> >   4
> >   >>> index_of('banana', ['apple', 'banana', 'cherry', 'date'])
> >   1
> >   >>> index_of(5, [2, 3, 4])
> >   -1
> >   >>> index_of('b', ['apple', 'banana', 'cherry', 'date'])
> >   -1
> > """
> > plek = 0
> > if type(seq) == type([]):
> > plek = seq.index(val)
> > elif type(seq) == type(()):
> > seq = list (seq)
> > plek = seq.index(val)
> > else :
> > plek = seq.find(val)
> > return plek
> 
> Not sure if this is correct but why don't you check for the index
> attribute? It is part of both lists and strings. Also you can use
> try/except to catch a ValueError. My version below, but I dislike the
> list() usage...
> 
> def index_of(val, seq, start=0):
> if hasattr(seq, 'index'):
> try:
> return seq.index(val, start)
> except ValueError:
> return -1
> else:
> try:
> return list(seq).index(val, start)
> except ValueError:
> return -1
> 
> > File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 70, in
> > __main__.index_of
> >
> > Failed example:
> >
> > index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
> >
> > Expected:
> >
> > 6
> >
> > Got:
> >
> > 3
> >
> > But in that tuple 5 is on position 3.
> >
> > Is the exercise here wrong ?
> 
> Looks like it, or it's a typo.
> 
> Greets
> Sander


Hello Sander, 

 

I agree that index is a part of string and list.

But not a part of a tuple.

As far as I know index is not a part of tuple so I have to convert it to a list 
so I can use index.

 

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


Re: [Tutor] exercise correct ??

2010-09-06 Thread Walter Prins
Hi Roelof,

On 6 September 2010 18:32, Roelof Wobben  wrote:

>>>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
>   6
>
> But in that tuple 5 is on position 3.
>
> Is the exercise here wrong ?
>
>

Not neccesarily...  I notice that the call is similar to the previous test
case, but has an extra parameter "4",  and that but that tuple that's being
searched contains other 5's.  If the extra parameter is interpreted as a
"starting index", then the next index of "5" starting at position 4, would
indeed be 6.  If my guesstimated intention of the test paramters is correct
then there's nothing wrong with the excercise/test and your implementation
of index_of needs to be enhanced to properly handle this extra parameter/new
test case.

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


Re: [Tutor] exercise correct ??

2010-09-06 Thread Sander Sweers
On 6 September 2010 19:32, Roelof Wobben  wrote:
> def index_of(val, seq, start=0):
>     """
>   >>> index_of(9, [1, 7, 11, 9, 10])
>   3
>   >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5))
>   3
>   >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
>   6
>   >>> index_of('y', 'happy birthday')
>   4
>   >>> index_of('banana', ['apple', 'banana', 'cherry', 'date'])
>   1
>   >>> index_of(5, [2, 3, 4])
>   -1
>   >>> index_of('b', ['apple', 'banana', 'cherry', 'date'])
>   -1
>     """
>     plek = 0
>     if type(seq) == type([]):
>     plek = seq.index(val)
>     elif type(seq) == type(()):
>     seq = list (seq)
>     plek = seq.index(val)
>     else :
>     plek = seq.find(val)
>     return plek

Not sure if this is correct but why don't you check for the index
attribute? It is part of both lists and strings. Also you can use
try/except to catch a ValueError. My version below, but I dislike the
list() usage...

def index_of(val, seq, start=0):
if hasattr(seq, 'index'):
try:
return seq.index(val, start)
except ValueError:
return -1
else:
try:
return list(seq).index(val, start)
except ValueError:
return -1

> File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 70, in
> __main__.index_of
>
> Failed example:
>
> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
>
> Expected:
>
> 6
>
> Got:
>
> 3
>
> But in that tuple 5 is on position 3.
>
> Is the exercise here wrong ?

Looks like it, or it's a typo.

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


[Tutor] exercise correct ??

2010-09-06 Thread Roelof Wobben

Hello, 

 

I have this programm :

 

def index_of(val, seq, start=0):
"""
  >>> index_of(9, [1, 7, 11, 9, 10])
  3
  >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5))
  3
  >>> index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
  6
  >>> index_of('y', 'happy birthday')
  4
  >>> index_of('banana', ['apple', 'banana', 'cherry', 'date'])
  1
  >>> index_of(5, [2, 3, 4])
  -1
  >>> index_of('b', ['apple', 'banana', 'cherry', 'date'])
  -1
"""
plek = 0 
if type(seq) == type([]):
plek = seq.index(val)
elif type(seq) == type(()):
seq = list (seq)
plek = seq.index(val)
else :
plek = seq.find(val)
return plek 


 

But I get this message :

 

File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 70, in 
__main__.index_of
Failed example:
index_of(5, (1, 2, 4, 5, 6, 10, 5, 5), 4)
Expected:
6
Got:
3

 

But in that tuple 5 is on position 3.

 

Is the exercise here wrong ?

 

Roelof

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


Re: [Tutor] exercise problem

2010-08-27 Thread Alan Gauld


"Dave Angel"  wrote


while teller < len(u):
getal1 = u[teller] + v[teller]
uitkomst = uitkomst + str(getal1)
But then I get a list of string instead of integers.


You're close.  Now that you've initialized the result variable to 
[], you can use + just as you're doing.  Just take out the str() 
function in that line.  You've still got duplicate names there 
between that function and the outer level code.


You will need to make the new result a list too for + to work, like 
so:


uitkomst = uitkomst + [getal1]

Or you can just append() the answer to uitkomst

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] exercise problem

2010-08-27 Thread Dave Angel



Roelof Wobben wrote:

Date: Fri, 27 Aug 2010 14:27:34 -0400
From: da...@ieee.org
To: rwob...@hotmail.com
CC: alan.ga...@btinternet.com; tutor@python.org
Subject: Re: [Tutor] exercise problem


The other problem is you're confusing the variables inside the function 
with the ones declared outside. While you're learning, you should use 
different names for the two sets of variables. So create a new variable 
inside the function, called something like result. Give it an initial 
value, a list of the desired size. Then assign to one of its elements 
each time through the loop. And don't forget to change the return 
statement to return that variable instead of the confused-named one.


DaveA





Hello, 

 

I put in right after getal1 = 

I have tried another solution nl. 

 


Put every outcome in a string.

Then convert the string into a list like this :

 


def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""
teller=0
getal1=0
uitkomst=""
while teller < len(u):
getal1 = u[teller] + v[teller]
uitkomst = uitkomst + str(getal1) 
teller=teller+1

uitkomst2 = list(uitkomst)
return uitkomst2
 
uitkomst= []

uitkomst2=[]
vector= [1, 2, 1], [1, 4, 3]
v=vector[0]
u=vector[1]
uitkomst = add_vectors(u,v)
print uitkomst 

 


But then I get a list of string instead of integers.

  
You're close.  Now that you've initialized the result variable to [], 
you can use + just as you're doing.  Just take out the str() function in 
that line.  You've still got duplicate names there between that function 
and the outer level code.


There's also no need to convert uitkomst to a list, since it already is.


DaveA

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


Re: [Tutor] exercise problem

2010-08-27 Thread Francesco Loffredo

We are close to the solution, keep trying!

On 27/08/2010 19.56, Roelof Wobben wrote:

Hello,

Now I have this :

def add_vectors(u, v):
"""
 >>> add_vectors([1, 0], [1, 1])
[2, 1]
 >>> add_vectors([1, 2], [1, 4])
[2, 6]
 >>> add_vectors([1, 2, 1], [1, 4, 3])
[2, 6, 4]
 >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
[13, -4, 13, 5]
"""
teller=0
getal1=0
getal2=0
while teller < len(u):
getal1 = u[teller] + v[teller]
teller=teller+1
return uitkomst2

uitkomst= []
uitkomst2=[]
vector= [1, 2, 1], [1, 4, 3]
v=vector[0]
u=vector[1]
uitkomst = add_vectors(u,v)
print uitkomst

The only problem I have is to build up uitkomst2.
There is no need to initialize uitkomst, because it will be created by 
the line

> uitkomst = add_vectors(u,v)
but you should create the list uitkomst2 INSIDE the add_vectors 
function. uitkomst2 is an internal storage for...

on every loop getal1 has the value of the outcome.

... that's it, for the different values that you put in getal1.
There is no more need for getal2, too.
So you could put the line
> uitkomst2=[]
in the place where you should delete
> getal2=0
that is just before the while loop.
And finally, in the loop, just before
> teller=teller+1
you need to insert the line that you haven't yet read about, but that 
you really need: (ta-dah!)


uitkomst2.append(getal1)

As you will (YOU WILL, WON'T YOU?) read in Alan's tutorial, and also in 
the one you're reading, this line extends the uitkomst2 list by adding a 
new element to its 'tail': the number getal1.

So, if you follow the flow of your function, you can see it happening:

-- BEGINNING ---
vector= [1, 2, 1], [1, 4, 3]  # maybe Python can forgive you, but
  # I would write [[1, 2, 1], [1, 4, 3]]
  # or ([1, 2, 1], [1, 4, 3]) ...
v=vector[0]  # now v = [1, 2, 1]
u=vector[1]  # now u = [1, 4, 3]
uitkomst = add_vectors(u,v)  # add_vectors is called to provide a value
teller=0
getal1=0
uitkomst2 = []
while teller < len(u):  # teller= 0, len(u) = 3, OK
getal1 = u[teller] + v[teller]  # getal1 = u[0]+v[0] = 1+1 = 2
uitkomst2.append(getal1)  # uitkomst2 is now [2]
teller=teller+1
while teller < len(u):  # teller= 1, len(u) = 3, OK
getal1 = u[teller] + v[teller]  # getal1 = u[1]+v[1] = 2+4 = 6
uitkomst2.append(getal1)  # uitkomst2 is now [2, 6]
teller=teller+1
while teller < len(u):  # teller= 2, len(u) = 3, OK
getal1 = u[teller] + v[teller]  # getal1 = u[2]+v[2] = 1+3 = 4
uitkomst2.append(getal1)  # uitkomst2 is now [2, 6, 4]
teller=teller+1
while teller < len(u):  # teller= 3, len(u) = 3, STOP!
return uitkomst2  # and finally the list uitkomst2 becomes the value
  # that the add_vectors function will provide.
uitkomst = add_vectors(u,v)  # now uitkomst becomes [2, 6, 4]
print uitkomst  # and lived happily ever after.



So I thought this would work

uitkomst2 [teller] = getal1

But then i get a out of range.

sure, because you tried to access an element in an empty list.



Roelof

Hope you got it, and keep trying!
Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3096 -  Data di rilascio: 
08/26/10 20:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise problem

2010-08-27 Thread Roelof Wobben


 

> Date: Fri, 27 Aug 2010 14:27:34 -0400
> From: da...@ieee.org
> To: rwob...@hotmail.com
> CC: alan.ga...@btinternet.com; tutor@python.org
> Subject: Re: [Tutor] exercise problem
> 
> (Don't top-post, it loses all the context)
> 
> Roelof Wobben wrote:
> > Hello, 
> >
> > 
> >
> > Now I have this :
> >
> > 
> >
> > def add_vectors(u, v):
> > """
> > >>> add_vectors([1, 0], [1, 1])
> > [2, 1]
> > >>> add_vectors([1, 2], [1, 4])
> > [2, 6]
> > >>> add_vectors([1, 2, 1], [1, 4, 3])
> > [2, 6, 4]
> > >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
> > [13, -4, 13, 5]
> > """
> > teller=0
> > getal1=0
> > getal2=0 
> > while teller < len(u):
> > getal1 = u[teller] + v[teller]
> > teller=teller+1
> > return uitkomst2
> > 
> > uitkomst= []
> > uitkomst2=[]
> > vector= [1, 2, 1], [1, 4, 3]
> > v=vector[0]
> > u=vector[1]
> > uitkomst = add_vectors(u,v)
> > print uitkomst 
> >
> >
> > The only problem I have is to build up uitkomst2.
> > on every loop getal1 has the value of the outcome.
> > So I thought this would work
> >
> > uitkomst2 [teller] = getal1 
> >
> > But then i get a out of range.
> >
> > 
> Where did you put that statement? Was it indented like getal1= and 
> teller= ? I doubt it. If you don't use the value till the loop is 
> over, then the subscript will be wrong, and so will the value.
> 
> The other problem is you're confusing the variables inside the function 
> with the ones declared outside. While you're learning, you should use 
> different names for the two sets of variables. So create a new variable 
> inside the function, called something like result. Give it an initial 
> value, a list of the desired size. Then assign to one of its elements 
> each time through the loop. And don't forget to change the return 
> statement to return that variable instead of the confused-named one.
> 
> DaveA
> 


Hello, 

 

I put in right after getal1 = 

I have tried another solution nl. 

 

Put every outcome in a string.

Then convert the string into a list like this :

 

def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""
teller=0
getal1=0
uitkomst=""
while teller < len(u):
getal1 = u[teller] + v[teller]
uitkomst = uitkomst + str(getal1) 
teller=teller+1
uitkomst2 = list(uitkomst)
return uitkomst2
 
uitkomst= []
uitkomst2=[]
vector= [1, 2, 1], [1, 4, 3]
v=vector[0]
u=vector[1]
uitkomst = add_vectors(u,v)
print uitkomst 

 

But then I get a list of string instead of integers.

 

You say I have to make a vector and put all the values into it.

That can work only you have to know how big the vector must be.

Or work with a lot of if then.

 

Roelof

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


Re: [Tutor] exercise problem

2010-08-27 Thread Dave Angel

(Don't top-post, it loses all the context)

Roelof Wobben wrote:
Hello, 

 


Now I have this :

 


def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""
teller=0
getal1=0
getal2=0 
while teller < len(u):

getal1 = u[teller] + v[teller]
teller=teller+1
return uitkomst2
 
uitkomst= []

uitkomst2=[]
vector= [1, 2, 1], [1, 4, 3]
v=vector[0]
u=vector[1]
uitkomst = add_vectors(u,v)
print uitkomst 



The only problem I have is to build up uitkomst2.
on every loop getal1 has the value of the outcome.
So I thought this would work

uitkomst2 [teller] = getal1 


But then i get a out of range.

  
Where did you put that statement?  Was it indented like getal1= and 
teller= ?  I doubt it.  If you don't use the value till the loop is 
over, then the subscript will be wrong, and so will the value.


The other problem is you're confusing the variables inside the function 
with the ones declared outside.  While you're learning, you should use 
different names for the two sets of variables.  So create a new variable 
inside the function, called something like result.  Give it an initial 
value, a list of the desired size.  Then assign to one of its elements 
each time through the loop.  And don't forget to change the return 
statement to return that variable instead of the confused-named one.


DaveA

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


Re: [Tutor] exercise problem

2010-08-27 Thread Roelof Wobben

Hello, 

 

Now I have this :

 

def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""
teller=0
getal1=0
getal2=0 
while teller < len(u):
getal1 = u[teller] + v[teller]
teller=teller+1
return uitkomst2
 
uitkomst= []
uitkomst2=[]
vector= [1, 2, 1], [1, 4, 3]
v=vector[0]
u=vector[1]
uitkomst = add_vectors(u,v)
print uitkomst 


The only problem I have is to build up uitkomst2.

 

on every loop getal1 has the value of the outcome.

 

So I thought this would work

 

uitkomst2 [teller] = getal1 

 

But then i get a out of range.

 

Roelof
 


Date: Fri, 27 Aug 2010 10:19:30 -0700
From: alan.ga...@btinternet.com
Subject: Re: [Tutor] exercise problem
To: rwob...@hotmail.com










>u v, result
 
first example.
 
u : [1.0]  v: [1,1] result [2.1] 
 


OK, Great, you got that.



first split  u en v in only numbers.



No, you should leave them as lists.





Then add u[0] en v[0] and u[1] and v[1] 
put the outcome in the new  vector.

Almost except you don't know how many elements there will be so 
you need a loop to process all the elements.








outcome= [outcome u, outcome[u]
return outcome.


This confused me, the output should be:

[ u[0]+v[0], u[1]+v[1], u[2]+v[2], , [u[n]+v[n] ]

And in case you are wondering, a vector is used in math to, 
for example, represent a point in space. A 2 dimensional 
point has an X,Y coordinate so we can create a 2 element 
vector: [x,y]

We can add, subtract and multiply vectors. Vectors can also 
be used to represent other physical measures, for example 
AC electric current has a magnitude and phase angle at any 
point in time. These two values can be combined as a vector.
We can use bigger vectors to represent, for example the 
set of inputs to a parallel port printer, so we would have a 
list of 8 binary values. Once again we can express mathematically 
the processing of this vector as a function and by applying the 
function to the vector deermine the expected output for any given input. 
That could be, for example, an ASCII character for the printer example...
They are very important in science and engineering.

The tutorial you are following does expect the reader to be quite 
math literate - it is part of a university comp sci course after all. If
you do not have a strong math background you may find some of 
the others more readable. For example mine( :-) ) does not assume 
any knowledge of math beyond basic high school level - really basic 
geometry and arithmetic.

Alan G.



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


Re: [Tutor] exercise problem

2010-08-27 Thread christopher . henk
Roelof Wobben wrote on 08/27/2010 12:18:01 PM:

> 
> 
> > Date: Fri, 27 Aug 2010 12:00:23 -0400
> > From: bgai...@gmail.com
> > To: rwob...@hotmail.com
> > CC: tutor@python.org
> > Subject: Re: [Tutor] exercise problem
> > 
> > I have been reading your posts and responses. I find myself frustrated 

> > with your lack of understanding of Python fundamentals and the time 
and 
> > energy others are putting in that seems to help only a little.
> > 
> > I recommend you take a very basic tutorial, and be sure you understand 

> > the basic concepts before tackling what seems too hard for you
> 
> 
> I follow this basic tutorial : 
http://openbookproject.net/thinkcs/python/english2e/ch09.html
> 
> 
> > 
> > Also I encourage you to take each error message and look up the topic. 

> > In this case
> > 
> > getal1 = getal1 + u[teller,0] + v[teller,0]
> > TypeError: list indices must be integers, not tuple
> > 
> > What does this error tell you?
> 
> That the list index must be a integer.
> 
> > 
> > What is a tuple? What is an integer? Where is there a tuple in this 
> > expression? What are you trying to do?
> 
> What a tuple is i can't tell you. it's in the next chapter.
> A integer is a number.
> What Im trying to do is that I want all the first numbers of the vectors 
are added and all the second numbers.
> 
> So if I have this vector [1.0] [1,1]

You seem still confused on how your parameters to your function are 
working or the assignment in general.  You do not have a vector [[1,0] 
[1,1]] being passed into your function.  You are passing in two lists 
(vectors).  The first one 'u' is [1,0] , the second 'v' is [1,1] 
part of your code below:
vector= [[1,0], [1,1]]
v=vector[0]
u=vector[1]
print u, v
uitkomst = add_vectors(u,v)

if you just print u it will show [0,1]
if you just print v it will print [1,1]

thus your code:
getal1 = getal1 + u[teller,0] + v[teller,0]
getal2 = getal2 + v[teller,1] + v[teller,1]

is not doing what you thought it would.  (the syntax is wrong also).  I am 
guessing you are trying to index a 2D matrix with this.  Section 9.18 
would show you how to do that in python.  However there is no 2D matrix in 
your function, there are 2 vectors u and v.
 
So to add the vectors you loop over the index numbers (see 9.13) in your 
tutorial. in this loop add the value referenced by the index in each list 
to each other (section 9.2), and assign it to your new list at the same 
index value (9.8).  The one thing that would catch you on the assignment 
part, is that the list you are assigning to must have an element at that 
index (I didn't see the append function mentioned in your tutorial yet). 
Therefore you must initialize the list to which you are assigning to make 
sure it is the same length as the vectors you are adding (9.12 gives a 
method for this). 
 
Hopefully that will get you a bit closer to your answer.  I would suggest 
spending time in this section and make sure you understand what each line 
is doing, since these concepts are fundamentals in python programming. 
Use the interactive session in python to quickly see what each command 
does.  Python is great in that it will give you instant feedback.

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


Re: [Tutor] exercise problem

2010-08-27 Thread Francesco Loffredo

On 27/08/2010 17.05, Roelof Wobben wrote:

Hello,

My first try :

def add_vectors(u, v):
"""
 >>> add_vectors([1, 0], [1, 1])
[2, 1]
 >>> add_vectors([1, 2], [1, 4])
[2, 6]
 >>> add_vectors([1, 2, 1], [1, 4, 3])
[2, 6, 4]
 >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
[13, -4, 13, 5]
"""
teller=1
getal1=0
getal2=0
while teller < len(u):


Up to this point, it's a good start.

Let's address the last problem first:
> uitkomst = add_vectors[u,v]
>
> But now I get this error message :
>
> uitkomst = add_vectors[u,v]
>
> TypeError: 'function' object is not subscriptable
>
> So it seems that I can't use vectors as a variable in a function.

No, it just seems that you used brackets [] instead of parentheses () in 
the function call. When you call a function, you must follow its name 
with parentheses, even when the function doesn't need arguments.

So this line should read:
uitkomst = add_vectors(u, v)
or, if you really (really?) think the two vectors should be enclosed in 
a bigger list:

uitkomst = add_vectors([u, v]).
This is just one of the problems, I'll address the rest when you'll find 
them. Just some hints:



getal1 = getal1 + u[teller,0] + v[teller,0] ???
getal2 = getal2 + v[teller,1] + v[teller,1] ???

> teller=teller+1

Ok, while I was writing, Walter told you about parentheses, so I'll 
address your next question:


But now Im getting this message :


Traceback (most recent call last):

File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 28, in 

uitkomst = add_vectors(u,v)

File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 17, in add_vectors

getal1 = getal1 + u[teller,0] + v[teller,0]
TypeError: list indices must be integers, not tuple


When you call add_vectors, you give it u and v as arguments, and they 
are two lists, as per your requirements:

v = vector[0]  makes  v equal to the list [1, 0], and
u = vector[1]  makes  u equal to the list [1, 1].
Inside add_vectors, you want to sum the coordinates of the two vectors, 
so you should refer to each of them with their position in the list.
Just as you did with the list called vector, you should use a single 
integer to address an element also in the u and v lists inside 
add_vectors. If you use a couple of numbers separated by a comma, as you 
did, Python reads them as a tuple, and it complains. So the 2 lines that 
sum the vectors should become a single one:


getal1 = getal1 + u[teller] + v[teller]

Then you don't even need to accumulate the sum in the variable getal1, 
because you are building a list and you can store each sum in a 
different position of the new list... remember the append method of the 
lists? So that line should read:


getal1 = u[teller] + v[teller]

and getal1 should be appended to a list... uitkomst2, perhaps?



Roelof


Francesco



return uitkomst2[getal1, getal2] ???
uitkomst= []
vector= [[1,0], [1,1]]
v=vector[0]
u=vector[1]
uitkomst = add_vectors[u,v]

But now I get this error message :

uitkomst = add_vectors[u,v]

TypeError: 'function' object is not subscriptable

So it seems that I can't use vectors as a variable in a function.

Roelof
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3096 -  Data di rilascio: 
08/26/10 20:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise problem

2010-08-27 Thread Roelof Wobben


 

> Date: Fri, 27 Aug 2010 12:00:23 -0400
> From: bgai...@gmail.com
> To: rwob...@hotmail.com
> CC: tutor@python.org
> Subject: Re: [Tutor] exercise problem
> 
> I have been reading your posts and responses. I find myself frustrated 
> with your lack of understanding of Python fundamentals and the time and 
> energy others are putting in that seems to help only a little.
> 
> I recommend you take a very basic tutorial, and be sure you understand 
> the basic concepts before tackling what seems too hard for you

 

 

I follow this basic tutorial : 
http://openbookproject.net/thinkcs/python/english2e/ch09.html

 


> 
> Also I encourage you to take each error message and look up the topic. 
> In this case
> 
> getal1 = getal1 + u[teller,0] + v[teller,0]
> TypeError: list indices must be integers, not tuple
> 
> What does this error tell you?

 

That the list index must be a integer.


> 
> What is a tuple? What is an integer? Where is there a tuple in this 
> expression? What are you trying to do?


What a tuple is i can't tell you. it's in the next chapter.

A integer is a number.

What Im trying to do is that I want all the first numbers of the vectors are 
added and all the second numbers.

 

So if I have this vector [1.0] [1,1]

I try to add 1 and 1 and after that I try to add 0 and 1 

and put the answer in a new vector.

 

 

> 
> Look up list indexing and see if you can solve it yourself, then return 
> with any questions that arise from this effort.
> 
> The more you do for yourself the faster and better you will learn and we 
> will be more encouraged to help.
> 
> How does this sound to you? Will you give it a try?


Of course I will give it a try. 

 

> 
> -- 
> Bob Gailer
> 919-636-4239
> Chapel Hill NC
> 

 

Roelof


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


[Tutor] exercise problem

2010-08-27 Thread Roelof Wobben


 Hello,
 
Thanks for pointing this out.
 
I change it to this :
 
def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""
teller=1
getal1=0
getal2=0 
while teller < len(u):
getal1 = getal1 + u[teller,0] + v[teller,0]
getal2 = getal2 + v[teller,1] + v[teller,1]
teller=teller+1
uitkomst2 = [getal1, getal2]
return uitkomst2
 
uitkomst= []
vector= [[1,0], [1,1]]
v=vector[0]
u=vector[1]
print u, v
uitkomst = add_vectors(u,v)
print uitkomst  
 
But now Im getting this message :
 


Traceback (most recent call last):
File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 28, in 
uitkomst = add_vectors(u,v)
File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 17, in add_vectors
getal1 = getal1 + u[teller,0] + v[teller,0]TypeError: list indices must be 
integers, not tuple
 
Roelof
 

 


Date: Fri, 27 Aug 2010 16:20:27 +0100
Subject: Re: [Tutor] exercise problem
From: wpr...@gmail.com
To: rwob...@hotmail.com
CC: tutor@python.org

Hi Roelof,

See below


On 27 August 2010 16:05, Roelof Wobben  wrote:



uitkomst = add_vectors[u,v]
 
But now I get this error message :
 

Traceback (most recent call last):File 
"C:\Users\wobben\workspace\oefeningen\src\test.py", line 27, in  
uitkomst = add_vectors[u,v]
TypeError: 'function' object is not subscriptable
 
So it seems that I can't use vectors as a variable in a function.
Carefully compare the syntax for calling your function (as in the doctest) to 
what you've written above.  See the difference?  (Hint: check the type of 
parentheses...)

The error message is giving you a hint -- a subscriptable item is something 
like a list or array.  They use square brackets.  Function calls always use 
(round) parentheses.  To python, it looks like you're trying to subcript the 
function object "add_vectors", which obviously isn't possible.  Hence the 
message.

Regards,

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


Re: [Tutor] exercise problem

2010-08-27 Thread bob gailer
 I have been reading your posts and responses. I find myself frustrated 
with your lack of understanding of Python fundamentals and the time and 
energy others are putting in that seems to help only a little.


I recommend you take a very basic tutorial, and be sure you understand 
the basic concepts before tackling what seems too hard for you.


Also I encourage you to take each error message and look up the topic. 
In this case


getal1 = getal1 + u[teller,0] + v[teller,0]
TypeError: list indices must be integers, not tuple

What does this error tell you?

What is a tuple? What is an integer? Where is there a tuple in this 
expression? What are you trying to do?


Look up list indexing and see if you can solve it yourself, then return 
with any questions that arise from this effort.


The more you do for yourself the faster and better you will learn and we 
will be more encouraged to help.


How does this sound to you? Will you give it a try?

--
Bob Gailer
919-636-4239
Chapel Hill NC

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


Re: [Tutor] exercise problem

2010-08-27 Thread Walter Prins
Hi Roelof,

See below

On 27 August 2010 16:05, Roelof Wobben  wrote:

>
> uitkomst = add_vectors[u,v]
>
> But now I get this error message :
>
>
> Traceback (most recent call last):
> *File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 27, in
> *
>
> uitkomst = add_vectors[u,v]
>
> TypeError: 'function' object is not subscriptable
>
>
>
> So it seems that I can't use vectors as a variable in a function.
>
Carefully compare the syntax for calling your function (as in the doctest)
to what you've written above.  See the difference?  (Hint: check the type of
parentheses...)

The error message is giving you a hint -- a subscriptable item is something
like a list or array.  They use square brackets.  Function calls always use
(round) parentheses.  To python, it looks like you're trying to subcript the
function object "add_vectors", which obviously isn't possible.  Hence the
message.

Regards,

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


Re: [Tutor] exercise problem

2010-08-27 Thread Roelof Wobben

Hello, 

 

My first try :

 

def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""
teller=1
getal1=0
getal2=0 
while teller < len(u):
getal1 = getal1 + u[teller,0] + v[teller,0]
getal2 = getal2 + v[teller,1] + v[teller,1]
teller=teller+1
return uitkomst2[getal1, getal2]

uitkomst= []
vector= [[1,0], [1,1]]
v=vector[0]
u=vector[1]
uitkomst = add_vectors[u,v]

 

But now I get this error message :

 

Traceback (most recent call last):
File "C:\Users\wobben\workspace\oefeningen\src\test.py", line 27, in 
uitkomst = add_vectors[u,v]
TypeError: 'function' object is not subscriptable
 
So it seems that I can't use vectors as a variable in a function.
 
Roelof
 
 
 

 


 


From: rwob...@hotmail.com
To: tutor@python.org
Subject: RE: [Tutor] exercise problem
Date: Fri, 27 Aug 2010 14:38:23 +




Oke, 
 
That's also the point Alan is making.
I try now to make  the function and puttting it on this maillist if it's ready.
 
Maybe I can learn more about efficient progamming or better way to do this.
 
Roelof

 
> From: st...@pearwood.info
> To: tutor@python.org
> Date: Sat, 28 Aug 2010 00:15:15 +1000
> Subject: Re: [Tutor] exercise problem
> 
> On Fri, 27 Aug 2010 08:23:06 pm Roelof Wobben wrote:
> > > > Write a function add_vectors(u, v) that takes two lists of
> > > > numbers
> [...]
> > My new idea is that u is the number which must be calculated and v is
> > the vector which containts the outcome or u is the outcome of the
> > first numbers and v the outcome of the second numbers. 
> 
> If you had a function called "add_numbers(x, y)", you would say that x 
> and y are the numbers which are to be added. If you have a function 
> called "add_vectors(u, v)", then u and v are the vectors to be added. 
> Perhaps this example will help:
> 
> 
> u = [1, 10, 100]
> v = [2, 11, 111]
> add_vectors(u, v)
> => [3, 21, 211]
> 
> 
> 
> 
> -- 
> Steven D'Aprano
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise problem

2010-08-27 Thread Roelof Wobben

Oke, 

 

That's also the point Alan is making.

I try now to make  the function and puttting it on this maillist if it's ready.

 

Maybe I can learn more about efficient progamming or better way to do this.

 

Roelof


 
> From: st...@pearwood.info
> To: tutor@python.org
> Date: Sat, 28 Aug 2010 00:15:15 +1000
> Subject: Re: [Tutor] exercise problem
> 
> On Fri, 27 Aug 2010 08:23:06 pm Roelof Wobben wrote:
> > > > Write a function add_vectors(u, v) that takes two lists of
> > > > numbers
> [...]
> > My new idea is that u is the number which must be calculated and v is
> > the vector which containts the outcome or u is the outcome of the
> > first numbers and v the outcome of the second numbers. 
> 
> If you had a function called "add_numbers(x, y)", you would say that x 
> and y are the numbers which are to be added. If you have a function 
> called "add_vectors(u, v)", then u and v are the vectors to be added. 
> Perhaps this example will help:
> 
> 
> u = [1, 10, 100]
> v = [2, 11, 111]
> add_vectors(u, v)
> => [3, 21, 211]
> 
> 
> 
> 
> -- 
> Steven D'Aprano
> ___
> Tutor maillist - Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise problem

2010-08-27 Thread Steven D'Aprano
On Fri, 27 Aug 2010 08:23:06 pm Roelof Wobben wrote:
> > > Write a function add_vectors(u, v) that takes two lists of
> > > numbers
[...]
> My new idea is that u is the number which must be calculated and v is
> the vector which containts the outcome or u is the outcome of the
> first numbers and v the outcome of the second numbers. 

If you had a function called "add_numbers(x, y)", you would say that x 
and y are the numbers which are to be added. If you have a function 
called "add_vectors(u, v)", then u and v are the vectors to be added. 
Perhaps this example will help:


u = [1, 10, 100]
v = [2, 11, 111]
add_vectors(u, v)
=> [3, 21, 211]




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


Re: [Tutor] exercise problem

2010-08-27 Thread Francesco Loffredo

On 27/08/2010 12.23, Roelof Wobben wrote:


From: rwob...@hotmail.com
To: alan.ga...@btinternet.com
Subject: RE: [Tutor] exercise problem
Date: Fri, 27 Aug 2010 07:04:39 +

 > To: tutor@python.org
 > From: alan.ga...@btinternet.com
 > Date: Thu, 26 Aug 2010 23:54:19 +0100
 > Subject: Re: [Tutor] exercise problem
 >
 > "Roelof Wobben"  wrote
 >
 > > Write a function add_vectors(u, v) that takes two lists of numbers
 >
 > > I think that u is the name of the new list and v is the number which
 > > represent the number which must be eveluated.
 >
 > No. It sounds like you don't really understand the basic concepts
 > behind functions yet. it does for me! :-)
 > ...
 > --
 > Alan Gauld
 > Author of the Learn to Program web site
 > http://www.alan-g.me.uk/

Hello,

I read your page and I think I understand the basic concepts.
What I don't see is what s and v represent.

My new idea is that u is the number which must be calculated and v is
the vector which containts the outcome or u is the outcome of the first
numbers and v the outcome of the second numbers.

Roelof

Ok, let's take a deep breath and start from the beginning:

First: a vector is a (usually small) ordered set of numbers that are 
taken together to represent some mathematical entity or physical 
quantity. For example, (3,1,7) can mean the position of an object in 
space, relative to a Cartesian 3-dimensional axis system.


Second: the sum of two vectors is defined as a new vector, whose 
coordinates (the elements of the vectors) are each the sum of the same 
coordinates of the two given vectors:

v1 = (a, b, c)
v2 = (x, y, z)
v1 + v2 = (a+x, b+y, c+z)

That said, Alan tried to point you to the very important concept that:
Third: a function usually produces a result in itself, using the values 
given as arguments (those inside the parentheses, in your case u and v).
And that result is usually assigned to a variable, or used directly, by 
the funcion call itself:

new_vector = add_vectors(u, v)
or
print add_vectors(u, v)

The function you want to write should sum two of these vectors, that can 
be represented in Python as tuples (or as lists, as your exercise told you):

first_vector = (3, 1, 7)
second_vector = (7, -1, 13)
result = add_vectors(first_vector, second_vector)
print result
(10, 0, 20)

So, if you are asked to write a function that "takes two lists of 
numbers", I thought that those two lists were vectors, called u and v, 
and that the result of the sum, the "new vector", would be the result 
produced by the function.


Hope this made it clearer
Francesco
Nessun virus nel messaggio in uscita.
Controllato da AVG - www.avg.com
Versione: 9.0.851 / Database dei virus: 271.1.1/3096 -  Data di rilascio: 
08/26/10 20:34:00
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise problem

2010-08-27 Thread Roelof Wobben


 


From: rwob...@hotmail.com
To: alan.ga...@btinternet.com
Subject: RE: [Tutor] exercise problem
Date: Fri, 27 Aug 2010 07:04:39 +





 
> To: tutor@python.org
> From: alan.ga...@btinternet.com
> Date: Thu, 26 Aug 2010 23:54:19 +0100
> Subject: Re: [Tutor] exercise problem
> 
> "Roelof Wobben"  wrote
> 
> > Write a function add_vectors(u, v) that takes two lists of numbers
> 
> > I think that u is the name of the new list and v is the number which
> > represent the number which must be eveluated.
> 
> No. It sounds like you don't really understand the basic concepts
> behind functions yet. Try reading the Modules and Functions topic
> in my tutorial. See if that clarifies things for you. Getting these 
> basic
> concepts right at the beginning is very important, don't try to rush 
> it.
> 
> If confused by one tutorial reading an alternative explanation can
> often help - at least it does for me! :-)
> 
> -- 
> 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

Hello, 
 
I read your page and I think I understand the basic concepts.
What I don't see is what s and v represent.
 
My new idea is that u is the number which must be calculated and v is the 
vector which containts the outcome or u is the outcome of the first numbers and 
v the outcome of the second numbers.
 
Roelof
 
  ___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] exercise problem

2010-08-26 Thread Alan Gauld

"Roelof Wobben"  wrote


Write a function add_vectors(u, v) that takes two lists of numbers



I think that u is the name of the new list and v is the number which
represent the number which must be eveluated.


No. It sounds like you don't really understand the basic concepts
behind functions yet. Try reading the Modules and Functions topic
in my tutorial. See if that clarifies things for you. Getting these 
basic
concepts right at the beginning is very important, don't try to rush 
it.


If confused by one tutorial reading an alternative explanation can
often help - at least it does for me! :-)

--
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] exercise problem

2010-08-26 Thread Emile van Sebille

On 8/26/2010 12:02 PM Roelof Wobben said...


Hello,

I have this exercise:

Lists can be used to represent mathematical vectors. In this exercise and 
several that follow you will write functions to perform standard operations on 
vectors. Create a file named vectors.py and write Python code to make the 
doctests for each function pass.
Write a function add_vectors(u, v) that takes two lists of numbers of the same 
length, and returns a new list containing the sums of the corresponding 
elements of each.


def add_vectors(u, v):
 """
   >>>  add_vectors([1, 0], [1, 1])
   [2, 1]
   >>>  add_vectors([1, 2], [1, 4])
   [2, 6]
   >>>  add_vectors([1, 2, 1], [1, 4, 3])
   [2, 6, 4]
   >>>  add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
   [13, -4, 13, 5]
 """

add_vectors should pass the doctests above



I think that u is the name of the new list and v is the number which represent 
the number which must be eveluated.


No.  u,v are the parameters names for the two lists of numbers of the 
same length.  So in the example add_vectors([1, 0], [1, 1]), u will take 
on the value [1, 0] and v the value [1, 1].


HTH,

Emile






Is this right or do I mis understood the exercise ?



Roelof






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



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


[Tutor] exercise problem

2010-08-26 Thread Roelof Wobben

Hello, 
 
I have this exercise:
 
Lists can be used to represent mathematical vectors. In this exercise and 
several that follow you will write functions to perform standard operations on 
vectors. Create a file named vectors.py and write Python code to make the 
doctests for each function pass.
Write a function add_vectors(u, v) that takes two lists of numbers of the same 
length, and returns a new list containing the sums of the corresponding 
elements of each.


def add_vectors(u, v):
"""
  >>> add_vectors([1, 0], [1, 1])
  [2, 1]
  >>> add_vectors([1, 2], [1, 4])
  [2, 6]
  >>> add_vectors([1, 2, 1], [1, 4, 3])
  [2, 6, 4]
  >>> add_vectors([11, 0, -4, 5], [2, -4, 17, 0])
  [13, -4, 13, 5]
"""

add_vectors should pass the doctests above

 

I think that u is the name of the new list and v is the number which represent 
the number which must be eveluated.

 

Is this right or do I mis understood the exercise ?

 

Roelof

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


Re: [Tutor] Exercise in writing a python function

2006-08-09 Thread Alan Gauld
> I confused things by my mistake in the specifications.
>
> The criterion for exiting the outer loop is that
>
> mpylist[0][1] * mult[0] + mpylist[1][1] * mult[1] + mpylist[2][1] * 
> mult[2]
> + . . .
>
> be > zlim.

Can you try rewriting the spec correctly, because I'm still not
sure I understand it? Are you now saying that mpylist[n][0] is never 
used?

Can you show us some sample data structures - just a few entries
and how the calculations should work?

Alan G. 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Exercise in writing a python function

2006-08-09 Thread Kermit Rose
  
Message: 2
Date: Wed, 9 Aug 2006 16:04:27 +1200
From: "John Fouhy" <[EMAIL PROTECTED]>
Subject: Re: [Tutor] Exercise in writing a python function.
To: "Tutor mailing list" 
 
 
Hi Kermit,
 
Your basic data structure is a list (actually, several related lists),
which you work your way through.  So I would start off with a for
loop:
 
for k in range(len(mult)):
 
Then, in the body of the loop, your basic logic is:
 
Add 1 to mult[k].
If mult[k] is not too big, exit.
Otherwise, ...
 
Hmm, actually, I'm not sure I do understand.  Does mpylist ever
change?  How is mult related to zlim?
 
--
John.
 
*
 
Thank you.
 
I confused things by my mistake in the specifications.
 
The criterion for exiting the outer loop is that
 
mpylist[0][1] * mult[0] + mpylist[1][1] * mult[1] + mpylist[2][1] * mult[2]
+ . . . 
 
be > zlim.
 
 
 
 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise in writing a python function.

2006-08-09 Thread Kermit Rose
 
 
From: Alan Gauld 
Date: 08/09/06 03:30:28 
To: Kermit Rose; tutor@python.org 
Subject: Re: [Tutor] Exercise in writing a python function. 
 
> The current specifications for the function are: 
> 
> def incr(mult,z,zlim,mpylist): 
> # mult is a vector of exponents for the multipliers in mpylist. 
> # z is a positive odd integer. 
> # zlim is the upper bound critical value for the sum of ( 
> mpylist[k][0] * 
> mpylist[k][1] ) 
 
Just to clarify what this means. 
you have mpylist = [(2,3),(3,5),(5,7)] 
So the products list is: [6,15,35] 
So zlim in this case should be greater than the 
sum of products: 6+15+35 = 56 
 
***
 
Oops.  That is what  I said.
 
I made a mistake in the specificiations.
 
I should have specified that 
for this example
that
3*mult[0] + 5 * mult[1] + 7 * mult[2] must be < zlim.
 
>>>>>
 
 
 
# where kth multiplier is mpylist [k][0] 
> # and kth multiplier index is mpylist [k][1] 
 
Not sure what you mean by the multiplier index? 
The sum above shows mpylist[k][1] being used in the multiplication, 
not as an index? 
 
 
**
 
Yes.  It's confusing because of my mistake of confusing mpylist[k][1] with
mult[k].
 
 
>>>>
 
> # function incr returns the next value of vector mult. 
 
I'm still not clear what mult does, your example above doesn't 
refer to mult anywhere? 
 
 
*
 
Yes, another consequence of my mistake in the specifications.
 
 
Kermit   <  [EMAIL PROTECTED]   >
 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise in writing a python function.

2006-08-09 Thread Alan Gauld
> The current specifications for the function are:
>
> def incr(mult,z,zlim,mpylist):
> # mult is a vector of exponents for the multipliers in mpylist.
> # z is a positive odd  integer.
> # zlim is the upper bound  critical value for the sum of  ( 
> mpylist[k][0] *
> mpylist[k][1] )

Just to clarify what this means.
you have mpylist = [(2,3),(3,5),(5,7)]
So the products list is: [6,15,35]
So zlim in this case should be greater than the
sum of products: 6+15+35 = 56

Have I got that right?

> # where kth multiplier is mpylist [k][0]
> # and kth  multiplier index is mpylist [k][1]

Not sure what you mean by the multiplier index?
The sum above shows mpylist[k][1] being used in the multiplication,
not as an index?

> # function incr returns the next value of vector mult.

I'm still not clear what mult does, your example above doesn't
refer to mult anywhere?

> # mult may be thought as a number written in a variable base.
> # mult[0] is the least significant and matches multiplier 
> mpylist[0][0]
> # adding one to mult would mean adding 1 to mult[0]
> # unless doing so would make sum of multiplier * index exceed zlim.
> # in that case mult[0] is set to zero, and 1 is added to mult[1]
> # unless doing so would make sum of multilier * index exceed zlim
> # in that case, mult[0] and mult[1] is set to zero,
> # and 1 is added to mult[2]
> # unless . . .

Sorry, you lost me there, can you provide a concrete example?
Or maybe some of our resident math experts recognise what
you are up to and can explain? Brian? Danny?

> # mult[0] is set to -1 to indicate that the largest possible value 
> of mult
> has been exceeded.
> # mpylist[0][0] = 2 and all other multipliers  in mpylist are odd.
> # mult[0], the index on multiplier 2, must not equal 1.  It may 
> equal zero,
> or any integer > 1,
> # provided the zlim constraint is met.

The only guidance I'd give in this kind of situation is that I'd adopt
functional programming approaches and try to copy the structure
of the spec in my code. This will be a bitch to test/debug unless
you have a wealth of manually validated examples to use as test
cases!

HTH

Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise in writing a python function.

2006-08-08 Thread John Fouhy
On 09/08/06, Kermit Rose <[EMAIL PROTECTED]> wrote:
> Hello John.
>
> Thanks for replying.
>
> In my previous version,  I used z as the critical value,.
>
> Since I created zlim,  a function of z, to be passed in as a parameter, I no
> longer need z
>
> in the parameter list.
>
> In another part of the program, z is multiplied by
>
> product of mpylist[ k][0] ** mult[k] for k  in range(len(mpylist)).
>
>
> I'm feeling something akin to writer's block when I sit down and attempt to
> start the code.
>
> I know that if I can just get started then the rest of the code will begin
> to flow in my thoughts.
>
> I did not really expect anyone to help me, but if you wish to  indicate the
> first few lines of
>
> either python or psuedo code
> for this routine, it probably will help me get started.
>
>
> Kermit   <  [EMAIL PROTECTED]  >

Hi Kermit,

Your basic data structure is a list (actually, several related lists),
which you work your way through.  So I would start off with a for
loop:

for k in range(len(mult)):

Then, in the body of the loop, your basic logic is:

Add 1 to mult[k].
If mult[k] is not too big, exit.
Otherwise, ...

Hmm, actually, I'm not sure I do understand.  Does mpylist ever
change?  How is mult related to zlim?

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Exercise in writing a python function.

2006-08-08 Thread John Fouhy
On 09/08/06, Kermit Rose <[EMAIL PROTECTED]> wrote:
> def incr(mult,z,zlim,mpylist):
> # mult is a vector of exponents for the multipliers in mpylist.
> # z is a positive odd  integer.
> # zlim is the upper bound  critical value for the sum of  ( mpylist[k][0] *
> mpylist[k][1] )
> # where kth multiplier is mpylist [k][0]
> # and kth  multiplier index is mpylist [k][1]
>
> # function incr returns the next value of vector mult.
> # mult may be thought as a number written in a variable base.
> # mult[0] is the least significant and matches multiplier mpylist[0][0]
> # adding one to mult would mean adding 1 to mult[0]
> # unless doing so would make sum of multiplier * index exceed zlim.
> # in that case mult[0] is set to zero, and 1 is added to mult[1]
> # unless doing so would make sum of multilier * index exceed zlim
> # in that case, mult[0] and mult[1] is set to zero,
> # and 1 is added to mult[2]
> # unless . . .
>
>
> # mult[0] is set to -1 to indicate that the largest possible value of mult
> has been exceeded.
> # mpylist[0][0] = 2 and all other multipliers  in mpylist are odd.
> # mult[0], the index on multiplier 2, must not equal 1.  It may equal zero,
> or any integer > 1,
> # provided the zlim constraint is met.

Hi Kermit,

Is there anything particular that you're stuck on?

I think I understand your function, although I'm not sure what role z
plays.  Are you looking for help on how to translate the description
you've given into logic suitable for programming?

-- 
John.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Exercise in writing a python function.

2006-08-08 Thread Kermit Rose
 Hello all.
 
 
I feel more familar with Python now, and when I recently went back to
reading
the tutorial,  I could actually read it without being overwhelmed by too
much new detail.
 
 
I've been staring at the specs for a python function for a while.
 
I wrote one version of it and it worked.
 
Then I changed the specs, and now need to reconstruct my original work.
 
I'm sure I can do it fairly easily.
 
I'm not asking anyone to write the function for me.
 
It did occur to me that it could be a classroom exercise for other students.
 
The current specifications for the function are:
 
 
def incr(mult,z,zlim,mpylist):
# mult is a vector of exponents for the multipliers in mpylist.
# z is a positive odd  integer.
# zlim is the upper bound  critical value for the sum of  ( mpylist[k][0] *
mpylist[k][1] ) 
# where kth multiplier is mpylist [k][0]
# and kth  multiplier index is mpylist [k][1]
 
# function incr returns the next value of vector mult.
# mult may be thought as a number written in a variable base.
# mult[0] is the least significant and matches multiplier mpylist[0][0]
# adding one to mult would mean adding 1 to mult[0]
# unless doing so would make sum of multiplier * index exceed zlim.
# in that case mult[0] is set to zero, and 1 is added to mult[1]
# unless doing so would make sum of multilier * index exceed zlim
# in that case, mult[0] and mult[1] is set to zero,
# and 1 is added to mult[2]
# unless . . .
 

# mult[0] is set to -1 to indicate that the largest possible value of mult
has been exceeded.
# mpylist[0][0] = 2 and all other multipliers  in mpylist are odd.
# mult[0], the index on multiplier 2, must not equal 1.  It may equal zero,
or any integer > 1, 
# provided the zlim constraint is met.
 
 
 
 
 
 

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor