Re: [Tutor] Extremely simple question

2012-01-11 Thread Noah Hall
On Wed, Jan 11, 2012 at 10:24 AM, Steven D'Aprano  wrote:
> Noah Hall wrote:
>>
>> On Wed, Jan 11, 2012 at 7:14 AM, Chris Johnson 
>> wrote:
>>>
>>> Hi there,
>>>
>>> I am *new* (I cannot put enough emphasis on that!) to Python programming,
>>> and to programming in general. I am trying to write out a statement that
>>> will protect a file on my computer from being run unless I enter the
>>> right
>>> specifications;
>>>
>>>> your_weight = int(raw_input("Please enter your weight: "))
>>>> if your_weight < 0:
>>>> print 'You're not Chris!'
>>>> elif your_weight == 170:
>>>> print 'You might be Chris! But...'
>>>> your_height = int(raw_input("Please enter your height: "))
>>>> if your_height < 180:
>>>> print 'You're not Chris!
>>>> elif your_height == 180:
>>>> print 'You're Chris!'
>>>> your_name = int(raw_input("What is your name? "))
>>>> elif your_height > 180:
>>>> print 'You're not Chris!"
>>>> elif x > 170:
>>>> print 'You're not Chris!'
>>>
>>>
>>> When I open it, the program says I have a syntax error. Praytell, where
>>> did
>>> I go wrong?
>>
>>
>> When you have a problem like this, you should copy and paste the
>> *whole* traceback here. Anyway, just from quickly looking, I think
>> your error lines in the the line
>>
>> elif x > 170:
>>
>> I'm sure you can work out why.
>
>
> Well, I can't. Chris reported that he gets a SYNTAX error, and for the life
> of me I can't work out what SyntaxError you think he'll be getting from that
> line.
>
> Based on Chris' post, he has a whole lot of code starting with ">", which of
> course will give a SyntaxError. He also hasn't indented his if/elif blocks.
>
> But of course his mail client might be mangling his code. Without being able
> to see the code, and the actual error message in full, we're all just
> guessing. And frankly, my crystal ball is out of order.

I had assumed that to him, a Syntax Error == any error, so guess it
was probably a name error as x isn't defined.

(Also, for me his post was indented correctly - probably down to my
client, though)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Extremely simple question

2012-01-10 Thread Noah Hall
On Wed, Jan 11, 2012 at 7:14 AM, Chris Johnson  wrote:
> Hi there,
>
> I am *new* (I cannot put enough emphasis on that!) to Python programming,
> and to programming in general. I am trying to write out a statement that
> will protect a file on my computer from being run unless I enter the right
> specifications;
>
>> your_weight = int(raw_input("Please enter your weight: "))
>> if your_weight < 0:
>> print 'You're not Chris!'
>> elif your_weight == 170:
>> print 'You might be Chris! But...'
>> your_height = int(raw_input("Please enter your height: "))
>> if your_height < 180:
>> print 'You're not Chris!
>> elif your_height == 180:
>> print 'You're Chris!'
>> your_name = int(raw_input("What is your name? "))
>> elif your_height > 180:
>> print 'You're not Chris!"
>> elif x > 170:
>> print 'You're not Chris!'
>
>
> When I open it, the program says I have a syntax error. Praytell, where did
> I go wrong?

When you have a problem like this, you should copy and paste the
*whole* traceback here. Anyway, just from quickly looking, I think
your error lines in the the line

elif x > 170:

I'm sure you can work out why.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] x%2

2012-01-10 Thread Noah Hall
On Tue, Jan 10, 2012 at 6:24 PM, emin  wrote:
> answers = ["yes","no"]
> reaction = ["OK.I GOT IT.But why symbol of percent % not symbol of division
> / ?","PLEASE EXPLAIN MORE"]
>
> print "1st SORRY FOR BAD ENGLISH & DISTURBING:((i am beginner)"
> print "So you want to say it doesnt mean 2 percent of x and it means x
> divisible by 2?"
> print "Yes"
> print "or"
> print "No"
> answerChoise = raw_input("Type here: ")
>
> if answerChoise == "yes" or answerChoise == "Yes":
>   print reaction[0]
>
> elif answerChoise == "no" or answerChoise == "No":
>   print reaction[1]

Urm, yeah, while this is cute and all, it's not a great way of asking
a question.

I'm not totally sure what you mean, so I'm just going to say -

a / b is the division operator. It divides things. It returns a divided by b.
a % b is the remainder operator. It returns what's "left" after dividing a by b.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] x%2

2012-01-09 Thread Noah Hall
2012/1/8 emin :
> http://www.youtube.com/watch?src_vid=QaYAOR4Jq2E&feature=iv&annotation_id=annotation_149056&v=M3g1GEkmyrw
> in this tutorial what does mean x%2 ?
> i think: i * 2% = always even number
> but why not 4,6 or 8? but  i * 4(6,8,10,12...)% = always even number too
> for example: 100 * 2(4,6,8,10,12...)% = 2(4,6,8,10,12...)
> even numbers = {2,4,6,8,10,12...}
> and how pyton understanding 0(even or odd number or it is an exception?)?

While it is true that all numbers divisible by 4 are even, not all
even numbers are divisible by 4. All even numbers, however, are
divisible by 2.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Python conundrum

2011-07-07 Thread Noah Hall
On Thu, Jul 7, 2011 at 3:17 PM, Lisi  wrote:
> Hi! :-)
>
> >From the book I am working from:
>
>  A shortcut is to do your import like this:
> from ex25 import  * 
>
> Is this a new and wonderful meaning of the word "shortcut"?  _Why_, _how_ is
> that a shortcut for:
>
> import ex25
>
> when it has seven *more* characters (counting the spaces)?  Or, put another
> way, twice as many words? :-/
>
> It is obviously an alternative, but I just don't get that it is a shortcut.
>
> Thanks,
> Lisi
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

Consider this -
Say I had a program that used a few functions from the math module -


import math

print math.sqrt(4)
print math.pow(2,2)
print math.factorial(5)
print math.cos(30)

Because the way import works, I would have to write math (to say "take
from the namespace math") before each function from math.
Using from math import *, I can instead write -

from math import *

print sqrt(4)
print pow(2,2)
print factorial(5)
print cos(30)

This imports the functions directly into the __main__ namespace
Now, over the course of my program, this would save hundreds of
characters, as you wouldn't need to type "math" before to state which
namespace to use.

(Of course, if using only certain functions from a module such as
math, you should really use from math import sqrt, pow, factorial,
cos)
- notice

>>> import math
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'math']

>>> from math import *
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'acos',
'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil
', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp',
'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frex
p', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma',
'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radian
s', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

>>> from math import factorial, pow, sqrt, cos
>>> dir()
['__builtins__', '__doc__', '__name__', '__package__', 'cos',
'factorial', 'pow', 'sqrt']

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


Re: [Tutor] Using a dict value in the same dict

2011-07-05 Thread Noah Hall
2011/7/5 Válas Péter :
> Hi,
>
> I have a dictionary with the keys 'a' and 'b'. It is not in a class. (I know
> that everything is in a class, but not explicitly.)
> May I use the value of 'a' when defining the value of 'b'? If so, what is
> the syntax?

Yes. The syntax is the same as anything involving a dict -

>>> a_dict = dict()
>>> a_dict['a'] = 2
>>> a_dict['b'] = a_dict['a'] + 1
>>> a_dict
{'a': 2, 'b': 3}
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Television

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

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

>def volume_down(tv, down = 1):

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

>if tv.volume > tv.volume_lowboundary :

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

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

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

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

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

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

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


Re: [Tutor] Television

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

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

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

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

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

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

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

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

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

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

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


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


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


Re: [Tutor] Defining a "format string"

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

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

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

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

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

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


Re: [Tutor] Defining a "format string"

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

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

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

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

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


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


Re: [Tutor] Television

2011-06-25 Thread Noah Hall
On Sat, Jun 25, 2011 at 9:18 AM, Vincent Balmori
 wrote:
>
> The question for this is to make a program that simulates a TV by creating it
> as an object. The user should be able to to enter a channel and or raise a
> volume. Make sure that the Channel Number and Volume stay within valid
> ranges. Before I can even start correcting my code this error shows up. I
> thought I had already created the "tv" instance.
> http://old.nabble.com/file/p31925053/Television Television
>
> Traceback (most recent call last):
>  File
> "/Users/vincentbalmori/Desktop/Python/py3e_source/chapter08/Television",
> line 83, in 
>    main()
>  File
> "/Users/vincentbalmori/Desktop/Python/py3e_source/chapter08/Television",
> line 46, in main
>    print(Television(tv.display))
> NameError: global name 'tv' is not defined

The problem is exactly as stated. There is no tv instance - it doesn't
exist. You can't call Television(tv.display) because there's simply
nothing called tv, and therefore it will not have a display method.
You're also calling the method incorrectly - if tv did exist, and it
had the method display, what you've done there is refer to the method,
and not call it. So, if you print it, you'd get something like



>>> def a():
... pass
...
>>> something = a
>>> print something

>>>

This isn't what you want, so remember to include brackets.

Instead, try -

tv = Television()
tv.display()

Note how there's no print on the tv.display()? That's because inside
Television.display, you've already told it to print, and told it to
return nothing (therefore None, by default), for example -

>>> def b():
... print 'This is something'
...
>>> print b()
This is something
None

See how None is printed?

Also, you should use better method functions - if you had called
"vold" "volume_down" instead, there would be no need for your comment
to explain what it is, and will result in less random guessing.

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


Re: [Tutor] Seattle PyCamp 2011

2011-06-19 Thread Noah Hall
> 1984 was not to be taken literally, of course. ;)
>
>
> Well, if you decide that in this day and age that asking whether
> someone knows how to use a browser to download files, or if someone
> knows how to install a program, then that's entirely up to you. I am
> merely in disbelief that you could find someone these days interested
> enough in computers to learn Python, and yet not know how to download
> a file. Had they been in jest, I would have understood, you know,
> something along the lines of "Want to learn Python? Well, there's only
> one thing you need to know - how to read!". But when taking it in
> seriousness, I must congratulate you on somehow finding these people;
> I had no idea they still existed. ;)
>
> Regards,
> Noah.

I also realised how aggressive my first reply was, for which I'm
sorry, I was merely trying to point out that perhaps they were out of
date questions.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Seattle PyCamp 2011

2011-06-19 Thread Noah Hall
On Sun, Jun 19, 2011 at 6:47 PM, Chris Calloway  wrote:
> On 6/17/2011 11:03 PM, Noah Hall wrote:
>>
>> On Sat, Jun 18, 2011 at 2:15 AM, Steven D'Aprano
>>  wrote:
>>>
>>> Noah Hall wrote:
>>>
>>>> Just a note, but are these questions jokes?
>>>>
>>>>> Know how to use a text editor (not a word processor, but a text
>>>>> editor)?
>>>>> Know how to use a browser to download a file?
>>>>> Know how to run a program installer?
>>>>
>>>> If not, then I'd consider removing them. This isn't 1984.
>>>
>>> I think the questions are fine. It indicates the level of technical
>>> knowledge required -- not much, but more than just the ability to sign in
>>> to
>>> AOL.
>>>
>>> In 1984 the newbies didn't know anything about computers *and knew they
>>> didn't know*, but now you have people who think that because they can
>>> write
>>> a letter in Microsoft Office and save as HTML, they're expert at
>>> programming.
>>>
>>> I wish I were joking but I've had to work for some of them.
>>
>> That's true, I suppose, but in that case the rest of the questions are
>> out of place.
>>
>> I believe that someone who knows what environmental variables are and
>> how to change them is a huge step up from someone who knows how to
>> *download things*.
>>
>
> Mr. Hall,
>
> I've taught Python to over a thousand students. And these questions, which
> are on the PyCamp site and not in the previous email to this list, are not
> only not the slightest bit out of place or jokes, but rather necessary. We
> didn't start out asking these questions of prospective students. They were
> developed from experience.
>
> As far as 1984, plenty of people in 1984 knew what environment variables
> were and how to change them without knowing how to use a browser to download
> anything. :) What is a "step up" is a matter of perspective. We get not a
> lot but plenty enough people coming to PyCamp whose last experience with
> using a computer was 1984. It's simple courtesy to warn those people of what
> to expect.
>
> Thank you for your concern.

1984 was not to be taken literally, of course. ;)


Well, if you decide that in this day and age that asking whether
someone knows how to use a browser to download files, or if someone
knows how to install a program, then that's entirely up to you. I am
merely in disbelief that you could find someone these days interested
enough in computers to learn Python, and yet not know how to download
a file. Had they been in jest, I would have understood, you know,
something along the lines of "Want to learn Python? Well, there's only
one thing you need to know - how to read!". But when taking it in
seriousness, I must congratulate you on somehow finding these people;
I had no idea they still existed. ;)

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


Re: [Tutor] Need script help with concept

2011-06-17 Thread Noah Hall
On Sat, Jun 18, 2011 at 2:13 AM, Corey Richardson  wrote:
> "Noah Hall"  wrote
>
>> Of course, if you mean *completely in-place replace* the question,
>> then you
>> will need something like ncurses (if on Linux)  -
>> http://docs.python.org/library/curses.html
>
> There also exists urwid, which is multiplatform, and is also less painful to
> use. I hear
> there is a port for Python 3 somewhere too. http://excess.org/urwid/
> (Apologies if email is HTML, using a web client)

I'm pretty sure that's not native to Windows; you'd have to use
Cygwin, in which case you might as well use curses.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Seattle PyCamp 2011

2011-06-17 Thread Noah Hall
On Sat, Jun 18, 2011 at 2:15 AM, Steven D'Aprano  wrote:
> Noah Hall wrote:
>
>> Just a note, but are these questions jokes?
>>
>>> Know how to use a text editor (not a word processor, but a text editor)?
>>> Know how to use a browser to download a file?
>>> Know how to run a program installer?
>>
>> If not, then I'd consider removing them. This isn't 1984.
>
> I think the questions are fine. It indicates the level of technical
> knowledge required -- not much, but more than just the ability to sign in to
> AOL.
>
> In 1984 the newbies didn't know anything about computers *and knew they
> didn't know*, but now you have people who think that because they can write
> a letter in Microsoft Office and save as HTML, they're expert at
> programming.
>
> I wish I were joking but I've had to work for some of them.

That's true, I suppose, but in that case the rest of the questions are
out of place.

I believe that someone who knows what environmental variables are and
how to change them is a huge step up from someone who knows how to
*download things*.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Seattle PyCamp 2011

2011-06-17 Thread Noah Hall
On Sat, Jun 18, 2011 at 12:46 AM, Chris Calloway  wrote:
> University of Washington Marketing and the Seattle Plone Gathering host the
> inaugural Seattle PyCamp 2011 at The Paul G. Allen Center for Computer
> Science & Engineering on Monday, August 29 through Friday, September 2,
> 2011.
>
> Register today at http://trizpug.org/boot-camp/seapy11/
>
> For beginners, this ultra-low-cost Python Boot Camp makes you productive so
> you can get your work done quickly. PyCamp emphasizes the features which
> make Python a simpler and more efficient language. Following along with
> example Python PushUps™ speeds your learning process. Become a
> self-sufficient Python developer in just five days at PyCamp! PyCamp is
> conducted on the campus of the University of Washington in a state of the
> art high technology classroom.


Just a note, but are these questions jokes?

>Know how to use a text editor (not a word processor, but a text editor)?
>Know how to use a browser to download a file?
>Know how to run a program installer?

If not, then I'd consider removing them. This isn't 1984.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Need script help with concept

2011-06-17 Thread Noah Hall
On Fri, Jun 17, 2011 at 11:34 PM, Victor  wrote:

> I am in the process of building a script but I do not know if what I am
> trying to do is possible. So, long story short, I need help.
>
> The concept:
> I am want to be able to ask the user a series of questions in the program
> window.
>
> But here is the action I want to appear on the screen.
>
> 0. Question pops up
> 1. User inputs answer
> 2. User press enter
> 3. The first question and answer is replaced with a new question on the
> screen
> 4. User answers the second question
> 5. User press enter
> 6. Question and answer disappears
> 7. etc.
>
> All user inputs are placed in a variable.
>
> Please help. :-)
>

One way I read this (not sure if this is what you meant, if so, skip down to
the bottom): Very possible, and very easy. Sounds a bit too much like a
homework question for me to give you complete help ;), however -

Look up raw_input.

>>> a_variable = raw_input('Is this a question?')
Is this a question?Yes
>>> print a_variable
Yes

Make a program, or attempt to, and post back here with any questions.


Of course, if you mean *completely in-place replace* the question, then you
will need something like ncurses (if on Linux)  -
http://docs.python.org/library/curses.html

If on Windows, have a look at - http://effbot.org/zone/console-index.htm

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


Re: [Tutor] checking if a variable is an integer?

2011-05-31 Thread Noah Hall
On Tue, May 31, 2011 at 10:36 PM, Rachel-Mikel ArceJaeger
 wrote:
> Isn't one of the unsolved millenium prize problems one that includes the
> ability to find all of the prime numbers? I'm not sure if your program is
> possible if the input number is large.
> But to check if a number x is an int, just do this:
> x == int(x)

As a note to the OP, this won't work without exception catching.
>>> 2 == int(2)
True
>>> 2 == int("fish")
Traceback (most recent call last):
  File "", line 1, in 
ValueError: invalid literal for int() with base 10: 'fish'


Of course, you can use the built-in string method isdigit() for this,
for example -
>>> "fish".isdigit()
False
>>> "2".isdigit()
True
>>> "2.0".isdigit()
False
>>> "2000".isdigit()
True
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Clunky Password maker

2011-05-25 Thread Noah Hall
On Wed, May 25, 2011 at 6:25 PM, Wolf Halton  wrote:
> Is there a less clunky way to do this?
> [code]
> def new_pass():
>     series = ['`', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-',
> '=', \
>               '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_',
> '+', \
>               'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']',
> '\\', \
>               'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '{', '}',
> '|', \
>               'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', "'", \
>               'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', '"', \
>               'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', \
>               'Z', 'X', 'C', 'V', 'B', 'N', 'M', '<', '>', '?']
>     passwd = []
>     p = input("Enter the length you want your password to be: ")
>               # length of password
>     for i in range(p):
>         r = random.randint(0, 94)
>         passwd.append(series[r]) # Append a random char from series[] to
> passwd
>     #print passwd
>     #print passwd[0], passwd[1], passwd[2], passwd[3]
>     print ""
>     print "".join(map(str, passwd)), " is your new password. \n"
> [/code]

I suggest you read up on the random module - there's two things that
you'll find immediately useful - random.choice and random.sample. I
suggest you use sample instead of the for loop you're using. Also,
when you print the output, there's no need to use map - everything in
your series.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Making a script part of the terminal

2011-05-20 Thread Noah Hall
On Fri, May 20, 2011 at 6:43 PM, michael scott  wrote:
> Okay, my title might be undescriptive, let me try to explain it better. I
> want to take a script I've written and make it usable by typing its name in
> the terminal. Perfect example is the python interpreter. You just type in
> the word python to the terminal and then the interpreter runs. I know other
> programs can do this as well (like mozilla or nautilus or rhythmbox).  So
> how do I make my scripts executable from the terminal?


Since you've mentioned Linux applications, I'm going to guess you mean
on Linux only. This is easy. In Unix scripts, we use something called
a shebang. A shebang is the very first line which basically tells the
terminal what program to use to run the script. For Python, it should
be "#! /usr/bin/python" (or where ever your python interpreter is, you
can find this out by using "which python")

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


Re: [Tutor] Error in executing 'Python Filename.py'.

2011-05-19 Thread Noah Hall
On Thu, May 19, 2011 at 9:14 PM, Neha P  wrote:
> C:\>python hello.py
> 'python' is not recognized as an internal or external command,
> operable program or batch file.

This happens because "python.exe" is not in the system path.

> C:\>cd python26
> C:\Python26>python
> Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)]
> on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
 python hello.py
>   File "", line 1
>     python hello.py
>                ^
> SyntaxError: invalid syntax

Now, this happens because you're in the python interpretor. Calling
python.exe will do this, unless you provide it with a file to
interpret. When you run this, you're already in the interpretor. Even
if it did work, it wouldn't because hello.py isn't in the path, unless
you've saved it to C:\Python26 or the location you've saved it to is
in the path.

 python
> Traceback (most recent call last):
>   File "", line 1, in 
> NameError: name 'python' is not defined

Again, see above.


> Above is the snapshot of the error.
> M running Windows 7 Home Premium, OS: NT
> Have  ;C:\Python26 added to the PATH variable , still unable to execute any
> file :( same error.
> Please help!! Thanks in advance..

Ah. Well, did you click OK to save it, then restart your shell? If you
did, try "echo %PATH%" if you're using cmd, or "$env:Path" if you're
using Powershell, and paste the output here.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Program

2011-05-19 Thread Noah Hall
On Thu, May 19, 2011 at 7:23 PM, Johnson Tran  wrote:
> So I figured out how to use the loop method, thanks. I still cannot seem to 
> figure out how to use Len() to show the output of my answers (from all my 
> googling Len() seems to be used to count characters?) Also, I am not really 
> sure I understand how to use the append method of the list.

That's true, that's exactly what len does (note, len, not Len). The
function you're looking for to sort the output is sorted().

append adds an item to the list, for example -
>>> names = ['John']
>>> names.append('Katie')
>>> names
['John', 'Katie']

> Here's my best guess so far:
>
>
> def CollectNames():
>
>    for attempt in range(1,6):
>        word=raw_input("Name #%d" % attempt)
>        list.append("new")

You need to create a list to append to, before you can do this.
If you want to use some form of validation, then this is where you
want len with set usage, for example

>>> names = ['Fred', 'Tom', 'Jake', 'Tom', 'Harry']
>>> names
['Fred', 'Tom', 'Jake', 'Tom', 'Harry']
>>> len(names)
5
>>> names_set = set(names)
>>> names_set
set(['Harry', 'Tom', 'Jake', 'Fred'])
>>> len(names_set)
4

Because "Tom" is in the list twice, and set has unique items only, the
length of the set is 4, not 5. This is where you can call whatever
validation you want (I suggest using a while loop).

>    print "All the names in alphabetical order are ", len(L);

This isn't where you want len. There's also nothing called "L" - is
this what you called your list? Also, L is a bad name for a variable.
Try to use descriptive names where possible.

>
> And just to recap, I was trying to get all the names outputted after the last 
> name was collected (and sort them in alphabetical order. Took my sort 
> commands out until I could figure out how to get any output first

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


Re: [Tutor] Sequencing

2011-05-19 Thread Noah Hall
On Thu, May 19, 2011 at 11:10 AM, Cindy Lee  wrote:
> Sorry I am still lost. So I am suppose to use a string? Something like:
> (just not sure is the right string...)

Well, no. The assignment you've been given states the you need to
define a function that takes a string as an argument, for example
>>> def take_string(some_string):
... return some_string
...
>>> take_string("Hello")
'Hello'

There, I have defined a function that takes a string as input, and
returns the same string unmodified.

What your assignment then says, however, is that you need to retrieve
numerical values from the string argument, modify them by adding one,
then return the new string.

So, to break it down for you -



1) Take a string value
2) Go through the string, taking each character one at a time. Create
an empty string to hold characters
3) If character is numerical, complete the number from the string and
add one to the total value, then skip the extra chars used to complete
the number, storing the new value in the empty string
4) If character is not numeric, store the character in the string
5) Return the created string
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Sequencing

2011-05-18 Thread Noah Hall
On Wed, May 18, 2011 at 11:27 AM, Cindy Lee  wrote:
> Thanks for the advice. I seem to keep getting the same errror:
>  Any advise? Also, is the _add_ string something I should be using?
 help(str.__add__)
> Help on wrapper_descriptor:
> __add__(...)
>     x.__add__(y) <==> x+y
No, not really. What you want to do is to parse the string given, and
then edit the values in place.

> 
> From: Dave Angel 
> To: Cindy Lee 
> Cc: "tutor@python.org" 
> Sent: Wednesday, 18 May 2011, 3:02
> Subject: Re: [Tutor] Sequencing
>
> On 01/-10/-28163 02:59 PM, Cindy Lee wrote:
>> Hi Pyton Tutors thanks for adding me,
>>
>> I am new to Python and missed one of my classes and am not sure of my
>> homework. We are currently on sequencing and are being asked to make a
>> function that receives text as an argument and returns the same text, but
>> with 1 added to each number. So far I have:
>>
>>
>> def ReceiveAndReturn():
>>
>>      sentence=raw_input("Give me a sentence with variables in it: ")
>>
>> print ReceiveAndReturn
>>
>>
>>
>> could anyone give me any hints on what else needs to be added?
>
> As for your code so far, you've omitted the parentheses in the call to
> ReceiveAndReturn.
>
>
>
> That's not a very complete assignment description.  Without some example
> text, I can only guess what these "sentences" might be permitted to look
> like.  So let me make a wild guess and see where it leads us.
>
> Suppose you assume that the numbers in the "sentence" will be unsigned
> (positive) integers, and that they will be separated from surrounding
> characters by whitespace.  That's a restrictive assumption, since a sentence
> might end with a number, and therefore there might be a period, not a space
> after it.  Similarly, a list of numbers might have commas, etc.  Assume also
> that extra spaces are irrelevant, so that one space between each word is
> fine.
>
> So you're looking to parse a 'sentence' like:
>
>   Joe had 44 baskets and 3 of them were broken.
>
> What you might want to do is split that string by whitespace, then loop
> through the resulting list, discovering any tokens that start with a digit.
> If it starts with a digit, convert it to an int, add one, and convert it
> back to a string.
>
> Then join the tokens (strings) back together into a single string (using a
> space character), and print it out.
>
> In my description I deliberately used several python keywords and library
> function names, so that you might be able to search them out in the python
> docs, to see how to apply them.
>
> DaveA
>
>
>
> ___
> 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] Dictionary File character reader

2011-05-09 Thread Noah Hall
On Tue, May 10, 2011 at 5:27 AM, Clara Mintz  wrote:
> Sorry I am completely new at python and don't understand why this function
> is returning an empty dictionary. I want it to take a list of files open
> them then return the number of characters as the value and the file name as
> the key.
> def fileLengths(files):
>     d = {}
>     files = []

Here's why. You take the name "files" and assign it to an empty list,
so you lose the "files" that you sent to the function.
>>> files = ["file_1.txt", "file_2.doc"]
>>> files
['file_1.txt', 'file_2.doc']
>>> files = []
>>> files
[]
You don't need that line at all, so remove it, and see what happens. :)

>     for file in files:
>         reader = open(file)
>         for line in reader:
>             char = sum(len(line))

As a side not, this doesn't need to be "sum" in this case - the len is
simply len, throwing away what it previously was. In fact, this
probably isn't what you want. At any one point, char is the length of
a single line, not the sum of every length of every line in the file.
What you want is something that takes the length of each line, and add
it to the sum. A simple way would be to do
>>>sum(len(line) for line in file)

>         d[file] = char
>         reader.close
>     return d
> Thank you sorry I don't understand what I am doing wrong.
> -Clara
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>

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


Re: [Tutor] Just started Python

2011-04-27 Thread Noah Hall
On Wed, Apr 27, 2011 at 9:17 PM, Johnson Tran  wrote:
> Thanks for the reply Alan and Noah, I really appreciate the help. I am really 
> trying to understand this although still cannot seem to grasp it all. I have 
> modified my program although now it seems to be giving me the wrong answer 
> with the correct answer when I input any value.
>
> I have program:
>
> model=raw_input("What kind of car do you drive?")
> gallons=raw_input("How many gallons have you driven?")
> number1 = float (gallons)
> miles=raw_input("How many miles have you driven?")
> number2 = float (miles)
>
>
> try:
>   model=float(model)
> except ValueError:
>   print "I cannot compute your total miles to gallon with those values."
> else:
>   print "Your average number of miles to gallons is",
> print number1 / number2
>
> Output:
>
> What kind of car do you drive?fire
> How many gallons have you driven?10
> How many miles have you driven?5
> I cannot compute your total miles to gallon with those values.

Again, it's because you're "validating" the wrong variable. model is
_not_ what you want to validate - it needs to be a string! gallons and
miles are what you want to validate. So try moving your try: except:
somewhere else, where it will validate the gallons and miles.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Just started Python

2011-04-27 Thread Noah Hall
On Wed, Apr 27, 2011 at 1:32 PM, Johnson Tran  wrote:
> Program:::
> model=raw_input("What kind of car do you drive?")
> number_string1=raw_input("How many gallons have you driven?")
> number1 = float (number_string1)

There's no validation for the variables when you try to convert them
(see bottom)

> number_string2=raw_input("How many miles have you driven?")
> number2 = float (number_string2)
>
>
> try:
>    model=float(model)
> except ValueError:
>    pass

Well, I'm not sure why you're trying to do this. You've obviously come
up with the error of it always raising exception due to the fact that
model is always going to be a string, so you've come up with a way to
avoid that. Why on earth would you want to always try to convert
something to float that you want as a string anyway? Think about it,
and see if you can figure out why this bit is all wrong.

> print "Your average number of miles to gallons is",
> print number1 / number2

> What kind of car do you drive?firebird
> How many gallons have you driven?30
> How many miles have you driven?60
> Your average number of miles to gallons is 0.5

> What kind of car do you drive?firebird
> How many gallons have you driven?test
>
> Traceback (most recent call last):
>  File "/Users/JT/Desktop/test", line 4, in 
>    number1 = float (number_string1)
> ValueError: invalid literal for float(): test

Your problem is that you're passing the string 'test' to the float
function. The string 'test' is not a valid value for the function
float, so it raises a ValueError - the Value that you're passing to
the function is wrong. In order to get around this, you'll want to
make sure that whatever you pass to the float function is a valid
value.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python2 vs python3 urllib

2011-04-23 Thread Noah Hall
On Sat, Apr 23, 2011 at 11:58 PM, pierre dagenais  wrote:
> The following code works as expected with python version 2.6.5, but with
> version 3.1.2 I get the following error:
>
> pierre:/MyCode/mesProjets$ py3 test.py
>
> Traceback (most recent call last):
>  File "test.py", line 3, in 
>    sock = urllib.urlopen("http://diveintopython.org/";)
> AttributeError: 'module' object has no attribute 'urlopen'
>
> The code:
>
> #example 8.5 of diveintopython.org/
> import urllib
> sock = urllib.urlopen("http://diveintopython.org/";)
> htmlSource = sock.read()
> sock.close()
> print (htmlSource)
>
> What is the proper syntax in version 3?

The problem is described in the exception - the module "urllib" has no
method urlopen. Things were moved around in 3.0. In fact,
diveintopython has some information on this here -
http://diveintopython3.org/porting-code-to-python-3-with-2to3.html#urllib
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Help - 2nd validator won't work

2011-04-16 Thread Noah Hall
On Sat, Apr 16, 2011 at 4:33 AM, Lea Parker  wrote:
>     budget = float(raw_input('Enter the amount of your budget for the month:
> '))
>
>     # Validation variable for budget
>
>     while budget <0:
>
>     print 'ERROR: the budget cannot be a negative amount'
>
>     budget = float(raw_input('Enter the correct budget for the month:
> '))
>

This is alright, but it still allows users to enter 0 for the budget


>     expense = float(raw_input('Enter your first expense '))
>
>     total_expense += expense

This is where you're going wrong. You don't validate the expense here
at all before adding it to total_expense.

> # Continue processing as long as the user does not enter 0
>
>     while expense != 0:
>     #Get another expense
>
>     expense = float(raw_input('Enter expense or 0 to finish '))
>
>     total_expense += expense
>
>     while expense <0:
>
>     print 'ERROR: the budget cannot be a negative amount'
>
>     expense = float(raw_input('Enter the correct budget for the
> month: '))
>
>     total_expense += expense

Same again here as above.

> #Calculate surplus
>
>     budget_difference = budget - total_expense
>

Now, given that total_expense could be negative, you'll end up with a
budget_difference that is in fact larger than the budget - crazy, I
know.

> # Call the main function.
>
> main()

It's also good practise to use
if __name__ == '__main__':
main()
So that you can then use this script for a module later on.

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


Re: [Tutor] Print images links

2011-04-12 Thread Noah Hall
On Tue, Apr 12, 2011 at 5:40 PM,   wrote:
> Hello,
>
> I would like to print the adresses of every image that are on the main page 
> of www.columbia.edu. Do you know how to do this?


Yes, I do. ;)

As this sounds like homework -
Firstly, you want to grab the source code of the webpage.
Secondly, you want to find what defines each image link - I'll give
you a clue, think of HTML markup for images.
Thirdly, use a regex to extract the link and either print it directly,
or put it into something you can reuse later.

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


Re: [Tutor] Static Variable in Functions

2011-03-14 Thread Noah Hall
On Mon, Mar 14, 2011 at 8:56 AM, Alan Gauld  wrote:
> "Yasar Arabaci"  wrote
>> Apperantly, I can change something (which is mutable) inside  a list
>> without even touching the list itself :)
> But the point is that you *are* touching the list.
> In this case you have two names referring to the same list.
> You can modify that list (because it is mutable) via either name, it
> makes no difference because they both refer to the same list.
>
> So a.append() is exactly the same operation as b.append()


Actually, in this case it's not - a.append() is the same as b[0].append() ;)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help

2011-03-08 Thread Noah Hall
On Tue, Mar 8, 2011 at 6:38 PM, Vickram  wrote:
> The python result is wrong because I may have misread the C++ code

Well, really, I suggest you read a tutorial on Python - you don't seem
to be getting a hang on the basics, for example, there's no need for
to use the float() function.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] help

2011-02-24 Thread Noah Hall
> I have found that this line will return an error every time while running
> the completed program, unless I enter a number. If I enter a numeric value
> the program will continue on as written.

When it comes to things like error messages, you need to post enough
code and the _exact_ error message in order for us to easily help you.

> name=input(“What is your name? “)

I imagine it's because you're using Python 2.*, where as the guide is
using the 3.* version.
The function you want in 2.7 is "raw_input", not "input". In 2.7,
"input" takes the string given then runs eval on it.
If you want to know more about this, read
http://docs.python.org/library/functions.html?#input and
http://docs.python.org/library/functions.html?#eval

Otherwise, you can either change your Python installation to Python
3.* to follow your guide, or you can carry on with 2.* and hope for
the best :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] python packaging systems

2011-02-10 Thread Noah Hall
On Thu, Feb 10, 2011 at 4:17 AM, Bill Allen  wrote:
>
> I have found there are a few systems available to package Python programs as
> standalone programs for distribution.   Do the folks here have any
> recommendation or comment on any of these?

My favourites are http://www.py2exe.org/ and http://www.pyinstaller.org/
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] 'Installing' Python at runtime? (Civilization)

2011-02-03 Thread Noah Hall
On Thu, Feb 3, 2011 at 11:11 AM, C.Y. Ruhulessin
 wrote:
> For an application that I am designing, i'd like to achieve the same
> functionality, so the end users don't have to bother installing Python
> themselves.
> Can anybody shed their lights on how one would program this?
> kind regards,


There are applications, such as http://www.py2exe.org/ and
http://www.pyinstaller.org/ that you can use to achieve the same
thing.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] noobie question,,,error related

2011-01-31 Thread Noah Hall
> I get this error when I try and load the datetime module,
>
> Traceback (most recent call last):
>   File "C:/Python27/TIMED_PROGRAM.py", line 2, in 
>     datetime.ctime()
> AttributeError: 'module' object has no attribute 'ctime'

Your problem, I guess comes from you having code like the following -

>>>import datetime
>>>datetime.ctime(arg)
Error

The problem with that is that datetime.ctime doesn't exist - it's
datetime.datetime.ctime.
If you want to skip the extra datetime in front, run

>>>from datetime import datetime
>>>datetime.ctime(arg)
Success.

In future, please provide us with code in order to clarify things.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Calaculating a monthly total of earning

2011-01-16 Thread Noah Hall
>     minus_weekends=hourly_income*64
>     total_income=int(minus_weekends-sum_of_monthly)

Here's problem number 1 - you're taking the values the wrong way around :)
It should be total_income=int(sum_of_monthly-minus_weekends)

>     sum_of_monthly=int(yearly_income/12)

Here's the second problem you encountered. The problem derives from
the fact that the operation is using two integers, which returns an
integer. Integer division and the int function rounds down, as it were
- 7/6 will give you 1,  int(5.9) will give you 5. This gives you 1
when you use 20, due to rounding down, whereas the answer you want
is 20/12.0 (1.6668). I suggest you don't use int here
yet, and use 12.0 instead of 12, in order to get a float as you are
performing further operations with the result, which will suffer due
to a loss of accuracy.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Explain to me Initializing

2011-01-15 Thread Noah Hall
Based on the number of emails you're sending to the mailing list, I
suggest you read a tutorial, work your way through it, and return with
any further issues you come across.

http://docs.python.org/tutorial/ is a good start, and I recommend any
of the tutorials on
http://wiki.python.org/moin/BeginnersGuide/NonProgrammers as a good
start.


Anyway, to answer your question, read
http://docs.python.org/tutorial/classes.html#class-objects
http://docs.python.org/reference/datamodel.html#object.__init__ and
http://docs.python.org/tutorial/modules.html#packages
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how come this doesnt work

2011-01-14 Thread Noah Hall
Adding reply to list -

On Sat, Jan 15, 2011 at 2:55 AM, walter weston  wrote:
> I only want to generate a random number once
>

Then you don't need a for loop. Think of a for loop as something you
need when you want to run a piece of code several times, for example

for x in range(1,6):
   print('This is message number %d' % (x))

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


Re: [Tutor] how come this doesnt work

2011-01-14 Thread Noah Hall
> import random
> for x in range(0,1):
>     num = random.random()
>     print (num)
>     m=input('input pass:')
>     if m==num:
>     print('you entered correctly, proceed')


Your problem lines in the differences in between the types - your num
variable is a float, whereas your m variable is a string. In order to
compare them here, you need to either use the float() function or the
str() function to convert one to a comparable data type, for example -

if m==str(num):
print('you entered correctly, proceed')

Might I also note that your for loop does nothing except run once, and
the 0 is unnecessary.

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


Re: [Tutor] syntax error that i cant spot!

2011-01-05 Thread Noah Hall
>
> "Please" and "Thank you" are rude? Oh my, have you lived a sheltered life
> :)
>

Nej, it was your condescension that I found rude. Also, you did it again,
perhaps on purpose though.. ;)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax error that i cant spot!

2011-01-05 Thread Noah Hall
>
> Please quote enough of the previous message to establish context -- when
> you are replying to the message, it is fresh in your mind. When others read
> your reply (possibly days later like I'm doing now), the context is anything
> but clear and your message comes across  as merely mysterious and obscure.


Certainly. In fact, it was an error in that message - I had deleted
accidentally that which I had quoted. Please be aware, however, rudeness
does nothing for you.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax error that i cant spot!

2011-01-02 Thread Noah Hall
He has no classes in there. Therefore, there is no place it should be in
this code. Please remember this is Python, and not Java nor anything else.
To quote directly from PEP 8, in regards to functions and variables,
 "should be lowercase, with words separated by underscoresas necessary to
improve readability.". Notice the word "readability" there ;). It's not a
big issue when you're working by yourself, but when working with multiple
people, or asking for help (like the OP here), you should stick to the
conventions of that language. it saves you time when you're trying to read
other people's code, it saves others' time when they try to read your code
and it saves time when they try and fix your code. It just makes life
easier.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] syntax error that i cant spot!

2011-01-01 Thread Noah Hall
It's part of the Python naming conventions laid out by PEP 8. Have a read
here - http://www.python.org/dev/peps/pep-0008/.

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


Re: [Tutor] syntax error that i cant spot!

2011-01-01 Thread Noah Hall
>
> > I am caught off guard but what is the purpose of the plus sign?  I don't
> recall seeing it used like that.
>

The + sign there is used for concating two strings together, for example -

output = 'foo' + 'bar'

Will give the variable output the value of the characters 'foobar'. This
also works with mixed values of variables holding strings and raw strings,
for example -

foo = 'foo'
output = foo + 'bar'

Which gives the same output as my previous example.

To OP:

I would not use the method you have used, but rather use string formatting,
so I would use on line 10 -

print 'Well, %s, I am thinking of a number between 1 & 20.' % (myName)

And on line 30 (which won't need the str(guessesTaken) on line 29) -

print 'Good job, %s! You guessed the number in %d guesses!' % (myName,
guessesTaken)

And then finally on line 34 -

print 'Nope. The number I was thinking of was %d' % (number)


By the way, for readability, tryToavoidCamelCase.
I also recommend you look into functions, classes, and exceptions - this
example is a good one with a lot of room to grow :)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] blank space after a number

2010-12-28 Thread Noah Hall
I'll just add there's a better way to do both of the examples you've done
there -


> > a, b = 0, 1
> > while b < 10:
> >print '%i%i' % (a,b) + ',',
> >b = b+1


An easier way of doing this is to instead do the following, including the a
(although not needed, as simply using 0 would work) -

a, b = 0, 1
while b <10:

print '%i%i,' % (a,b),
b += 1

> while b < 100:
> > print '%i' % (b) + ',',
> > b = b+1
>

while b < 100:

print '%i,' % (b),
b += 1

Or, using generators and a *for *loop (which is more suited to the task than
a *while *loop):

def stringify(x):

if x < 10:

return '0' + str(x)

else:

return str(x)

print ','.join(stringify(x) for x in xrange(1,101))
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Opening and closing SQLite db

2010-12-28 Thread Noah Hall
>
> > But to be sure, it is perfectly safe and valid to open the database on
> program startup, commit changes during the process and close it

> on exit (or unhandled exception)?


As long as it makes sense to do so, yes. There's no point having an open
connection to a database if there doesn't need to be.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Opening and closing SQLite db

2010-12-26 Thread Noah Hall
>
> >Which is suggestible just in case the app or program crashes during use.
>

(To O.P)
Indeed, though in such cases you must rely on your programmers instinct to
make the right decision - what applies for certain instances of an
application doesn't always conform with what applies for other instances.
For example, committing every event is hardly ideal for something such as a
media player, but may be correct for something handling vital data to the
system. There's no "one fits all feet" rule for such things - as you program
and develop more and more applications and programs, listen to your users,
and your bug tests. It'll come almost naturally after a while.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Opening and closing SQLite db

2010-12-26 Thread Noah Hall
As a rule, I tend to open and close a connection based on what I'm doing
with the database. If I were to directly run a series of queries one after
another, I would keep it open until a change in interface (referring to
changes in objects, both masters and slaves).
Also, you can perform any query that does not change any records safely
without a commit, for example, searches. For everything else, you need to
call commit - but you don't need to close the connection.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] A class list

2010-12-24 Thread Noah Hall
Based on what you initally stated, that you would have a list containing 500
numbers, and for each number, there would be a player, using a dict would be
ideal. For example, once you've got the winning number, how do you then
intend on handling what happens to each player as they win? Do you intend to
forget them? The player index need not be a name - it could be a number,
simply referring to the player, and then storing each number that player
stores in a list, for example

players = {1:[0,0,0,0,0]n:[0,0,0,0,0]}

This is just some food for thought.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor