Re: [Tutor] Editing html using python

2010-02-15 Thread Stefan Behnel
Luke Paireepinart, 14.02.2010 13:12:
> Also I think beautifulsoup is part of the Python standardlib now, isn't it?

No, it's not.

Stefan

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


Re: [Tutor] A Stuborn Tab Problem in IDLE

2010-02-15 Thread Alan Gauld


"Wayne Watson"  wrote

Thanks for the reminder on that. I haven't need the DOS box for 8 
months. Just off on other non-programming efforts for the most part. 
Things have picked up of late. I was beginning to think for awhile that 
Win7 might have dropped it. I don't see any changes to it.


Its worth running HELP on CMD and reading through it.

MS usually add a few extra tricks with each new OS.
And if nothing else it will remind you of the settings and 
registry tweaks you need to get the most out of the DOS box.

The default settings are very limited.

Alan G.

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


Re: [Tutor] Editing html using python

2010-02-15 Thread Alan Gauld


"Luke Paireepinart"  wrote

with BeautifulSoup were.  Also I think beautifulsoup is part of the 
Python

standardlib now, isn't it?  Why do you think it is not maintained?


I think you may be getting confused with element tree which is part
of the standard lib? BS is still an add-on.

Alan G. 



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


Re: [Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

2010-02-15 Thread patrice laporte
2010/2/14 Luke Paireepinart 

> I see why you would want the error messages but why is the default error
> message not enough, that is why I am curious, and typically introspection on
> objects is not necessary (for example, people often want to convert a string
> into a variable name to store a value (say they have the string "foobar1"
> and they want to store the value "f" in the variable "foobar1", how do they
> change foobar1 to reference a string?  well you can just use exec but the
> core issue is that there's really no reason to do it in the first place,
> they can just use a dictionary and store dict['foobar1'] = 'f'  and it is
> functionally equivalent (without the danger in the code)).  I get the
> feeling that your issue is the same sort of thing, where an easier solution
> exists but for whatever reason you don't see it.  I don't know if this is
> true or not.  Here's my take on this:
>
> >>> class x(object):
> def __init__(self, fname):
> self.temp = open(fname).read()
>
>
> >>> a = x('foobar')
>
> Traceback (most recent call last):
>   File "", line 1, in# this is the
> module it's in
> a = x('foobar')#
> this is the line where I tried to initialize it
>   File "", line 3, in __init__  # which called
> this function, which is the one that has the error
> self.temp = open(fname).read()  #and the error
> occurred while trying to perform this operation
> IOError: [Errno 2] No such file or directory: 'foobar'  #and the error was
> that the file 'foobar' could not be found.
>
>
Hi and thank to everybody...

First of all, I consider my first question is now answered : I wanted to get
rid of that sys._getframe call, I got an explanation (thanks to Kent).

The rest of the discussion is not about Python, it's more about the way of
thinking how to help the user having à good feeling with your app.

I try to clarify my need and share you my anxiety. Of course, a lot of thing
are available with Python, from a coder point of view. But what I want to do
is to think about the user, and give him a way to understand that what he
did was wrong.

Traceback give me all I need, but my opinion is that it's not acceptable to
give it back to the user without a minimum of "décorating". I didn't yet
look at the logging module, and maybe it can help me to make that
décorating.

And the user must be a priority (it's still my conviction here)

My own experience is that there is too much coder that forget the app they
work on is aim to be used by "real human", not by C/C++/Python/put what ever
you want here/ guru : if your app popups to the user a message that is just
what the traceback gave, it's not a good thing : How can it be reasonable to
imagine the user will read that kinda message ? :

*Traceback (most recent call last):
  File "", line 1, in 
a = x('foobar')
  File "", line 3, in __init__
self.temp = open(fname).read()
IOError: [Errno 2] No such file or directory: 'foobar'
*

Of course the origin of his problem is in the message : "*No such file or
directory: 'foobar'*", but a customer will never read that @ù^$#é uggly
message, there is too much extraterrestrial words in it.

Traceback doesn' give more thant that, it doesn't say, as an example : we
(the name of app) was trying to open the file "foobar" in order to do
something with it (put here what it was supposed to do with the file) : app
failed to open it because "foobar" doen't exist.

According to me, traceback is what we need during "coding" phases, but it's
not something to give to the user.

This problem has to be solved by thinking the app in the way I'm trying to
explain  (but not only in that way) : think about the user. This is not
something I expect Python to do for me, I'm just looking for everything
Python can provide me to make me think about the user.

I'm new to Python, and I make a lot of exploration to understand and answer
myself to my question. Python library is huge, and I don't have as enough
time as I wanted to deal with it. But I'm conviced the solutions are here, I
don't try to re-invent the wheel...


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


Re: [Tutor] Editing html using python

2010-02-15 Thread Amit Sethi
Well ya I was kind of hoping to know about more tools and recommendations on

how to edit broken html . Their were tags that I wanted to replace using
beautifulSoup
however i saw that the replacewith function was not available for many of
the tags I
wanted to replace . Also BeautifulSoup does not have unicode support .
So in a sense I did want recommendation perhaps I should have put my
question
in a better manner .
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Problem with "input" in Python 3

2010-02-15 Thread pja

Hi!

I am trying to teach myself how to program in Python using Zelle's 
"Python Programming: An Introduction to Computer Science" (a very good 
text). At the same time I have decided to start with Python 3 (3.1.1). 
That means that I have to convert Zelle's example code to Python 3 
(which generally I cope with).


I'm hoping that somebody can help with what's probably a very simple 
problem. There is a quadratic equation example involving multiple user 
inputs from the one "input" statement. The code works fine with Python 
2.5 but when I convert it to Python 3 I get error messages. The code 
looks like:


05 import math
06
07 def main():
08 print("This program finds the real solutions to a quadratic\n")
09
10 a, b, c = input("Please enter the coefficients (a, b, c): ")
11
12 '''
13 a = int(input("Please enter the first coefficient: "))
14 b = int(input("Please enter the second coefficient: "))
15 c = int(input("Please enter the third coefficient: "))
16 '''
17
18 discrim = b * b - 4 * a * c
19 ...

25 main()

Lines 08 to 12 are my Python 3 working solution but line 06 does not 
work in Python 3. When it runs it produces:


Please enter the coefficients (a, b, c): 1,2,3
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", 
line 25, in 
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", 
line 10, in main

builtins.ValueError: too many values to unpack
>>>

Clearly the problem lies in the input statement. If I comment out line 
10 and remove the comments at lines 12 and 16 then the program runs 
perfectly. However, I feel this is a clumsy solution.


Could somebody please guide me on the correct use of "input" for 
multiple values.


Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6

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


[Tutor] Problem with "input" in Python 3

2010-02-15 Thread Peter Anderson

Hi!

I am trying to teach myself how to program in Python using Zelle's 
"Python Programming: An Introduction to Computer Science" (a very good 
text). At the same time I have decided to start with Python 3 (3.1.1). 
That means that I have to convert Zelle's example code to Python 3 
(which generally I cope with).


I'm hoping that somebody can help with what's probably a very simple 
problem. There is a quadratic equation example involving multiple user 
inputs from the one "input" statement. The code works fine with Python 
2.5 but when I convert it to Python 3 I get error messages. The code 
looks like:


05 import math
06
07 def main():
08 print("This program finds the real solutions to a quadratic\n")
09
10 a, b, c = input("Please enter the coefficients (a, b, c): ")
11
12 '''
13 a = int(input("Please enter the first coefficient: "))
14 b = int(input("Please enter the second coefficient: "))
15 c = int(input("Please enter the third coefficient: "))
16 '''
17
18 discrim = b * b - 4 * a * c
19 ...

25 main()

Lines 08 to 12 are my Python 3 working solution but line 06 does not 
work in Python 3. When it runs it produces:


Please enter the coefficients (a, b, c): 1,2,3
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", 
line 25, in 
File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", 
line 10, in main

builtins.ValueError: too many values to unpack
>>>

Clearly the problem lies in the input statement. If I comment out line 
10 and remove the comments at lines 12 and 16 then the program runs 
perfectly. However, I feel this is a clumsy solution.


Could somebody please guide me on the correct use of "input" for 
multiple values.


Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread Christian Witts

pja wrote:

Hi!

I am trying to teach myself how to program in Python using Zelle's 
"Python Programming: An Introduction to Computer Science" (a very good 
text). At the same time I have decided to start with Python 3 (3.1.1). 
That means that I have to convert Zelle's example code to Python 3 
(which generally I cope with).


I'm hoping that somebody can help with what's probably a very simple 
problem. There is a quadratic equation example involving multiple user 
inputs from the one "input" statement. The code works fine with Python 
2.5 but when I convert it to Python 3 I get error messages. The code 
looks like:


05 import math
06
07 def main():
08 print("This program finds the real solutions to a quadratic\n")
09
10 a, b, c = input("Please enter the coefficients (a, b, c): ")
11
12 '''
13 a = int(input("Please enter the first coefficient: "))
14 b = int(input("Please enter the second coefficient: "))
15 c = int(input("Please enter the third coefficient: "))
16 '''
17
18 discrim = b * b - 4 * a * c
19 ...

25 main()

Lines 08 to 12 are my Python 3 working solution but line 06 does not 
work in Python 3. When it runs it produces:


Please enter the coefficients (a, b, c): 1,2,3
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 
3.2\src\debug\tserver\_sandbox.py", line 25, in 
File "C:\Program Files\Wing IDE 101 
3.2\src\debug\tserver\_sandbox.py", line 10, in main

builtins.ValueError: too many values to unpack
>>>

Clearly the problem lies in the input statement. If I comment out line 
10 and remove the comments at lines 12 and 16 then the program runs 
perfectly. However, I feel this is a clumsy solution.


Could somebody please guide me on the correct use of "input" for 
multiple values.


Regards,
Peter

You will need to split your input.

no_input = True
while no_input:
   in = input("Please enter the coefficients (a, b, c): ")
   try:
   a, b, c = map(int, in.split(','))
   no_input = False
   except ValueError:
   print("Please enter your input in a comma-seperated format with 
no space.  Only 3 numbers allowed")


That will loop allowing input until the person enters 3 numbers 
seperated purely by commas in for input.


--
Kind Regards,
Christian Witts
Business Intelligence

C o m p u s c a n | Confidence in Credit

Telephone: +27 21 888 6000
National Cell Centre: 0861 51 41 31
Fax: +27 21 413 2424
E-mail: cwi...@compuscan.co.za

NOTE:  This e-mail (including attachments )is subject to the disclaimer 
published at: http://www.compuscan.co.za/live/content.php?Item_ID=494.
If you cannot access the disclaimer, request it from 
email.disclai...@compuscan.co.za or 0861 514131.

National Credit Regulator Credit Bureau Registration No. NCRCB6 



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


Re: [Tutor] Editing html using python

2010-02-15 Thread Stefan Behnel
Amit Sethi, 15.02.2010 13:28:
> Well ya I was kind of hoping to know about more tools and recommendations on
> how to edit broken html.

You already found lxml, don't think you can do any better. Note that you
shouldn't "edit broken html" but "fix broken HTML and then edit correct
HTML". Writing out broken HTML after editing it is likely not what you want.


> Their were tags that I wanted to replace using beautifulSoup
> however i saw that the replacewith function was not available for many of
> the tags I wanted to replace.

To understand your problem better, it would be helpful if you provided more
information, such as the names of the tags and a short example code snippet
that showed what you were trying to do.


> Also BeautifulSoup does not have unicode support.

Try to learn a tool before making blatantly false claims about it.


> So in a sense I did want recommendation perhaps I should have put my
> question in a better manner.

That's a very good idea. This will help:

http://catb.org/~esr/faqs/smart-questions.html

Stefan

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


Re: [Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

2010-02-15 Thread Kent Johnson
On Mon, Feb 15, 2010 at 6:07 AM, patrice laporte  wrote:

> My own experience is that there is too much coder that forget the app they
> work on is aim to be used by "real human", not by C/C++/Python/put what ever
> you want here/ guru : if your app popups to the user a message that is just
> what the traceback gave, it's not a good thing : How can it be reasonable to
> imagine the user will read that kinda message ? :
>
> Traceback (most recent call last):
>   File "", line 1, in 
>     a = x('foobar')
>   File "", line 3, in __init__
>     self.temp = open(fname).read()
> IOError: [Errno 2] No such file or directory: 'foobar'
>
> Of course the origin of his problem is in the message : "No such file or
> directory: 'foobar'", but a customer will never read that @ù^$#é uggly
> message, there is too much extraterrestrial words in it.

It's easy to give the user just the actual error message, either the full
 IOError: [Errno 2] No such file or directory: 'foobar'
or slightly friendlier:
No such file or directory: 'foobar'

Then log the full trackback to a file. If the user needs help, ask
them to send you the log file. They never see the full traceback and
you get all the information available.

> Traceback doesn' give more thant that, it doesn't say, as an example : we
> (the name of app) was trying to open the file "foobar" in order to do
> something with it (put here what it was supposed to do with the file) : app
> failed to open it because "foobar" doen't exist.

In a GUI app the user will often know what was happening, because they
asked to do it.

Often it is more useful to give context from the point where the
exception is caught because that is a higher-level location than the
point of failure. When you catch the exception you can tell the user
 Error processing 
 No such file or directory: 'foobar'

This will make more sense than the low-level details of the error.

> According to me, traceback is what we need during "coding" phases, but it's
> not something to give to the user.
>
> This problem has to be solved by thinking the app in the way I'm trying to
> explain  (but not only in that way) : think about the user. This is not
> something I expect Python to do for me, I'm just looking for everything
> Python can provide me to make me think about the user.

This is a good goal. I'm not sure it requires wrapping the exception
mechanism to achieve it.

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


Re: [Tutor] Editing html using python

2010-02-15 Thread Kent Johnson
On Mon, Feb 15, 2010 at 7:28 AM, Amit Sethi  wrote:
> Well ya I was kind of hoping to know about more tools and recommendations on
> how to edit broken html .

This page lists several alternatives: html5, lxml, elementtree:
http://www.crummy.com/software/BeautifulSoup/3.1-problems.html

> Their were tags that I wanted to replace using
> beautifulSoup
> however i saw that the replacewith function was not available for many of
> the tags I
> wanted to replace . Also BeautifulSoup does not have unicode support .

??
http://www.crummy.com/software/BeautifulSoup/documentation.html#Beautiful%20Soup%20Gives%20You%20Unicode,%20Dammit

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread Kent Johnson
On Mon, Feb 15, 2010 at 7:34 AM, pja  wrote:

> 05 import math
> 06
> 07 def main():
> 08 print("This program finds the real solutions to a quadratic\n")
> 09
> 10 a, b, c = input("Please enter the coefficients (a, b, c): ")
> 11
> 12 '''
> 13 a = int(input("Please enter the first coefficient: "))
> 14 b = int(input("Please enter the second coefficient: "))
> 15 c = int(input("Please enter the third coefficient: "))
> 16 '''
> 17
> 18 discrim = b * b - 4 * a * c
> 19 ...
>
> 25 main()
>
> Lines 08 to 12 are my Python 3 working solution but line 06 does not work in
> Python 3. When it runs it produces:
>
> Please enter the coefficients (a, b, c): 1,2,3
> Traceback (most recent call last):
> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line
> 25, in 
> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py", line
> 10, in main
> builtins.ValueError: too many values to unpack

>
> Clearly the problem lies in the input statement. If I comment out line 10
> and remove the comments at lines 12 and 16 then the program runs perfectly.
> However, I feel this is a clumsy solution.
>
> Could somebody please guide me on the correct use of "input" for multiple
> values.

Python 3 input() is the same as Python 2 raw_input() - the result is a
string. You could use
a, b, c = ast.literal_eval(input("Please enter the coefficients (a, b, c): "))

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread Wayne Werner
On Mon, Feb 15, 2010 at 6:09 AM, Peter Anderson <
peter.ander...@internode.on.net> wrote:

> Hi!
>
> I am trying to teach myself how to program in Python using Zelle's "Python
> Programming: An Introduction to Computer Science" (a very good text). At the
> same time I have decided to start with Python 3 (3.1.1). That means that I
> have to convert Zelle's example code to Python 3 (which generally I cope
> with).
>
> I'm hoping that somebody can help with what's probably a very simple
> problem. There is a quadratic equation example involving multiple user
> inputs from the one "input" statement. The code works fine with Python 2.5
> but when I convert it to Python 3 I get error messages. The code looks like:
>
> 05 import math
> 06
> 07 def main():
> 08 print("This program finds the real solutions to a quadratic\n")
> 09
> 10 a, b, c = input("Please enter the coefficients (a, b, c): ")
> 11
> 12 '''
> 13 a = int(input("Please enter the first coefficient: "))
> 14 b = int(input("Please enter the second coefficient: "))
> 15 c = int(input("Please enter the third coefficient: "))
> 16 '''
> 17
> 18 discrim = b * b - 4 * a * c
> 19 ...
>
> 25 main()
>
> Lines 08 to 12 are my Python 3 working solution but line 06 does not work
> in Python 3. When it runs it produces:
>
> Please enter the coefficients (a, b, c): 1,2,3
> Traceback (most recent call last):
> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py",
> line 25, in 
> File "C:\Program Files\Wing IDE 101 3.2\src\debug\tserver\_sandbox.py",
> line 10, in main
> builtins.ValueError: too many values to unpack
> >>>
>
> Clearly the problem lies in the input statement. If I comment out line 10
> and remove the comments at lines 12 and 16 then the program runs perfectly.
> However, I feel this is a clumsy solution.
>
> Could somebody please guide me on the correct use of "input" for multiple
> values.
>

First off, it's much healthier to use "raw_input", and then try to convert
it (float, int, whatever).

Example:
In [1]: x = input("Test: ")
Test: print "Ha ha, this is some input"

   File "", line 1
 print "Ha ha, this is some input"
 ^
SyntaxError: invalid syntax

In [3]: x = raw_input("Test: ")
Test: print "ha ha, this is some raw_input"

In [4]: x
Out[4]: 'print "ha ha, this is some raw_input"'

The first one is susceptible to people inserting malicious code (at least in
pre-3.0 versions).

The reason your code is throwing an error is input (or raw_input) gives you
one value, and you're trying to assign it to three variables.

If you want to get 3 floating point values, the most concise (but maybe not
so readable if you're not familiar with the syntax) is probably this:

a, b, c = [float(x) for x in raw_input("Please enter (a, b, c): ").split()]

Example:

In [6]: a, b, c = [float(x) for x in raw_input("Please enter (a, b, c):
").split()]
Please enter (a, b, c): 3.1 2.99 1

In [7]: a
Out[7]: 3.1001

In [8]: b
Out[8]: 2.9902

In [9]: c
Out[9]: 1.0

to understand what the above code is doing:

print raw_input("Please enter (a,b, c): ")
print raw_input("Please enter (a,b, c): ").split()
print [x for x in raw_input("Please enter (a,b, c): ").split()]
print [float(x) for x in raw_input("Please enter (a,b, c): ").split()]

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


Re: [Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

2010-02-15 Thread Dave Angel

patrice laporte wrote:

2010/2/14 Luke Paireepinart 

  

I see why you would want the error messages but why is the default error
message not enough, that is why I am curious, and typically introspection on
objects is not necessary (for example, people often want to convert a string
into a variable name to store a value (say they have the string "foobar1"
and they want to store the value "f" in the variable "foobar1", how do they
change foobar1 to reference a string?  well you can just use exec but the
core issue is that there's really no reason to do it in the first place,
they can just use a dictionary and store dict['foobar1'] = 'f'  and it is
functionally equivalent (without the danger in the code)).  I get the
feeling that your issue is the same sort of thing, where an easier solution
exists but for whatever reason you don't see it.  I don't know if this is
true or not.  Here's my take on this:



class x(object):
  

def __init__(self, fname):
self.temp = open(fname).read()




a = x('foobar')
  

Traceback (most recent call last):
  File "", line 1, in# this is the
module it's in
a = x('foobar')#
this is the line where I tried to initialize it
  File "", line 3, in __init__  # which called
this function, which is the one that has the error
self.temp = open(fname).read()  #and the error
occurred while trying to perform this operation
IOError: [Errno 2] No such file or directory: 'foobar'  #and the error was
that the file 'foobar' could not be found.




Hi and thank to everybody...

First of all, I consider my first question is now answered : I wanted to get
rid of that sys._getframe call, I got an explanation (thanks to Kent).

The rest of the discussion is not about Python, it's more about the way of
thinking how to help the user having à good feeling with your app.

I try to clarify my need and share you my anxiety. Of course, a lot of thing
are available with Python, from a coder point of view. But what I want to do
is to think about the user, and give him a way to understand that what he
did was wrong.

Traceback give me all I need, but my opinion is that it's not acceptable to
give it back to the user without a minimum of "décorating". I didn't yet
look at the logging module, and maybe it can help me to make that
décorating.

And the user must be a priority (it's still my conviction here)

My own experience is that there is too much coder that forget the app they
work on is aim to be used by "real human", not by C/C++/Python/put what ever
you want here/ guru : if your app popups to the user a message that is just
what the traceback gave, it's not a good thing : How can it be reasonable to
imagine the user will read that kinda message ? :

*Traceback (most recent call last):
  File "", line 1, in 
a = x('foobar')
  File "", line 3, in __init__
self.temp = open(fname).read()
IOError: [Errno 2] No such file or directory: 'foobar'
*

Of course the origin of his problem is in the message : "*No such file or
directory: 'foobar'*", but a customer will never read that @ù^$#é uggly
message, there is too much extraterrestrial words in it.

Traceback doesn' give more thant that, it doesn't say, as an example : we
(the name of app) was trying to open the file "foobar" in order to do
something with it (put here what it was supposed to do with the file) : app
failed to open it because "foobar" doen't exist.

According to me, traceback is what we need during "coding" phases, but it's
not something to give to the user.

This problem has to be solved by thinking the app in the way I'm trying to
explain  (but not only in that way) : think about the user. This is not
something I expect Python to do for me, I'm just looking for everything
Python can provide me to make me think about the user.

I'm new to Python, and I make a lot of exploration to understand and answer
myself to my question. Python library is huge, and I don't have as enough
time as I wanted to deal with it. But I'm conviced the solutions are here, I
don't try to re-invent the wheel...


Thant to you all.

  
This makes lots of sense.  If the message doesn't make sense to the 
user, there's no point.  But why then is your thread titled "Getting 
caller name" ?  Why does the user care about the caller (function) 
name?  When you started the thread, it seemed clear that your user was a 
programmer, presumably who was adding code to your system and who wanted 
to see context error messages in coding terms.


If you have a duality of users, consider using a "DEBUG" variable, that 
changes the amount of detail you display upon an error.


DaveA

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread pja

Christian,

You are a genius! There was one minor bug; the variable "in" needed to 
be changed to something like "in_value" and it works perfectly.


Many thanks for that.

Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread Dave Angel

Peter Anderson wrote:

Hi!

I am trying to teach myself how to program in Python using Zelle's 
"Python Programming: An Introduction to Computer Science" (a very good 
text). At the same time I have decided to start with Python 3 (3.1.1). 
That means that I have to convert Zelle's example code to Python 3 
(which generally I cope with).


I'm hoping that somebody can help with what's probably a very simple 
problem. There is a quadratic equation example involving multiple user 
inputs from the one "input" statement. The code works fine with Python 
2.5 but when I convert it to Python 3 I get error messages. The code 
looks like:


05 import math
06
07 def main():
08 print("This program finds the real solutions to a quadratic\n")
09
10 a, b, c = input("Please enter the coefficients (a, b, c): ")
11
12 '''
13 a = int(input("Please enter the first coefficient: "))
14 b = int(input("Please enter the second coefficient: "))
15 c = int(input("Please enter the third coefficient: "))
16 '''
17
18 discrim = b * b - 4 * a * c
19 ...

25 main()

Lines 08 to 12 are my Python 3 working solution but line 06 does not 
work in Python 3. When it runs it produces:


Please enter the coefficients (a, b, c): 1,2,3
Traceback (most recent call last):
File "C:\Program Files\Wing IDE 101 
3.2\src\debug\tserver\_sandbox.py", line 25, in 
File "C:\Program Files\Wing IDE 101 
3.2\src\debug\tserver\_sandbox.py", line 10, in main

builtins.ValueError: too many values to unpack
>>>

Clearly the problem lies in the input statement. If I comment out line 
10 and remove the comments at lines 12 and 16 then the program runs 
perfectly. However, I feel this is a clumsy solution.


Could somebody please guide me on the correct use of "input" for 
multiple values.


Regards,
Peter
The input() function in Python3 produces a string, and does not evaluate 
it into integers, or into a tuple, or whatever.  See for yourself by trying


  print ( repr(input("prompt ")) )

on both systems.


You can subvert Python3's improvement by adding an eval to the return value.
  a, b, c = eval(input("Enter exactly three numbers, separated by commas"))

is roughly equivalent to Python 2.x  input expression.  (Python 3's 
input is equivalent to Python 2.x  raw_input)


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


Re: [Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

2010-02-15 Thread patrice laporte
2010/2/15 Dave Angel 

>
> This makes lots of sense.  If the message doesn't make sense to the user,
> there's no point.  But why then is your thread titled "Getting caller name"
> ?  Why does the user care about the caller (function) name?  When you
> started the thread, it seemed clear that your user was a programmer,
> presumably who was adding code to your system and who wanted to see context
> error messages in coding terms.
>
> If you have a duality of users, consider using a "DEBUG" variable, that
> changes the amount of detail you display upon an error.
>
> DaveA
>
>
No, it's not, I don't have duality at all... As I said in a previous answer,
when I started the thread, I was looking for a way not to use sys._getframe.
And I have now both the answer and an explanation.

But someone asked me "why do you want to do this ? explain it to us." And I
started to explain, and the thread then moved to new horizon, driffting to
other consideration : the user.

To answer you, the user I think about is the one who use the application,
not the programmer. But yes, the user who need to know the "caller name" is
the programmer, and that programmer must think himself as the other user,
the first one. This is just my point of view : never forget the final user.

So, maybe we can close that thread now ?  But any new question/answer is
wellcome

thanks.

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread pja

Kent,

Thanks! That works well!

Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6

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


Re: [Tutor] Editing html using python

2010-02-15 Thread Stefan Behnel
Kent Johnson, 15.02.2010 14:05:
> On Mon, Feb 15, 2010 at 7:28 AM, Amit Sethi wrote:
>> Well ya I was kind of hoping to know about more tools and recommendations on
>> how to edit broken html .
> 
> This page lists several alternatives: html5, lxml, elementtree:
> http://www.crummy.com/software/BeautifulSoup/3.1-problems.html

Except that elementtree isn't an alternative as it can't parse HTML. The
ElementSoup module uses the BS parser to create an ElementTree, not the
other way round. That's reported incorrectly on that page.

Stefan

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


[Tutor] Input() is not working as expected in Python 3.1

2010-02-15 Thread Yaraslau Shanhin
Hello All,

I am working with Python tutorial in wiki and one of the exercises is as
follows:

Ask the user for a string, and then for a number. Print out that string,
that many times. (For example, if the string is hello and the number is 3 you
should print out hellohellohello.)

Solution for this exercise is:

text = str(raw_input("Type in some text: "))
number = int(raw_input("How many times should it be printed? "))print
(text * number)



Since in Python raw_input() function was renamed to input() according
to PEP 3111  I have
respectively updated this code to:


text = str(input("Type in some text: "))
number = int(input("How many times should it be printed? "))print
(text * number)



However when I try to execute this code in Python 3.1 interpreter
error message is generated:


Type in some text: some
How many times should it be printed? 3
Traceback (most recent call last):
  File "test4.py", line 2, in 
number = int(input("How many times should it be printed? "))
ValueError: invalid literal for int() with base 10: 'How many times
should it be printed? 3'


Can you please advise me how to resolve this issue?
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Input() is not working as expected in Python 3.1

2010-02-15 Thread Wayne Werner
On Mon, Feb 15, 2010 at 8:23 AM, Yaraslau Shanhin <
yaraslau.shan...@gmail.com> wrote:

> Hello All,
>
> I am working with Python tutorial in wiki and one of the exercises is as
> follows:
>
> Ask the user for a string, and then for a number. Print out that string,
> that many times. (For example, if the string is hello and the number is 3 you
> should print out hellohellohello.)
>
> Solution for this exercise is:
>
> text = str(raw_input("Type in some text: "))
> number = int(raw_input("How many times should it be printed? "))print (text * 
> number)
>
>
>
> Since in Python raw_input() function was renamed to input() according to PEP 
> 3111  I have respectively updated 
> this code to:
>
>
> text = str(input("Type in some text: "))
>
>
This str() is redundant - input returns a string by default.


>
> number = int(input("How many times should it be printed? "))print (text * 
> number)
>
>
>
> However when I try to execute this code in Python 3.1 interpreter error 
> message is generated:
>
>
> Type in some text: some
> How many times should it be printed? 3
> Traceback (most recent call last):
>   File "test4.py", line 2, in 
> number = int(input("How many times should it be printed? "))
> ValueError: invalid literal for int() with base 10: 'How many times should it 
> be printed? 3'
>
>
That means you're having an issue with getting something that isn't just a
number.

try:

number = input("How many ... ")
print (number)
number = int(number)
print (number)

HTH,
Wayne


>
> Can you please advise me how to resolve this issue?
>
>
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>


-- 
To be considered stupid and to be told so is more painful than being called
gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness,
every vice, has found its defenders, its rhetoric, its ennoblement and
exaltation, but stupidity hasn’t. - Primo Levi
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Input() is not working as expected in Python 3.1

2010-02-15 Thread Dave Angel

Yaraslau Shanhin wrote:

Hello All,

I am working with Python tutorial in wiki and one of the exercises is as
follows:

Ask the user for a string, and then for a number. Print out that string,
that many times. (For example, if the string is hello and the number is 3 you
should print out hellohellohello.)

Solution for this exercise is:

text = str(raw_input("Type in some text: "))
number = int(raw_input("How many times should it be printed? "))print
(text * number)



Since in Python raw_input() function was renamed to input() according
to PEP 3111  I have
respectively updated this code to:


text = str(input("Type in some text: "))
number = int(input("How many times should it be printed? "))print
(text * number)



However when I try to execute this code in Python 3.1 interpreter
error message is generated:


Type in some text: some
How many times should it be printed? 3
Traceback (most recent call last):
  File "test4.py", line 2, in 
number = int(input("How many times should it be printed? "))
ValueError: invalid literal for int() with base 10: 'How many times
should it be printed? 3'


Can you please advise me how to resolve this issue?

  
When I correct for your missing newline, it works for me.  I don't know 
of any version of Python which would copy the prompt string into the 
result value of input or raw_input() function.


Try pasting the exact console session, rather than paraphrasing it.

DaveA

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


Re: [Tutor] Input() is not working as expected in Python 3.1

2010-02-15 Thread bob gailer

Yaraslau Shanhin wrote:

> Hello All,

Hello.

Suggestion for future questions - just include enough to identify the 
problem. We don't need to know that this is from a tutorial or what the 
exercise is. Also (at least I prefer) plain text without formatting or 
color.


Sufficient therefore is:

---
using  Python 3.1:
text = str(input("Type in some text: "))
number = int(input("How many times should it be printed? "))

Traceback (most recent call last):
File "test4.py", line 2, in 
   number = int(input("How many times should it be printed? "))
ValueError: invalid literal for int() with base 10:
---

Now carefully read the message "invalid literal for int()". This tells 
you the argument to int is not a string representation of an integer. 
There is nothing wrong with input()!


Since we don't know what you typed at the input() prompt we can't 
diagnose it further. One way to solve problems like these is to capture 
and print the output of input():


text = str(input("Type in some text: "))
snumber = input("How many times should it be printed?")
print snumber

A good robust alternative is to try int() and capture / report the failure:

text = str(input("Type in some text: "))
snumber = input("How many times should it be printed?")
try:
 number = int(snumber)
except ValueError:
 print(number, "is not an integer.")
else:
 print (text * number)

--
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] Input() is not working as expected in Python 3.1

2010-02-15 Thread Yaraslau Shanhin
Code:

text = str(input("Type in some text: "))
number = int(input("How many times should it be printed? "))
print (text * number)

Output:
Type in some text: some
How many times should it be printed? 5
Traceback (most recent call last):
  File "test4.py", line 2, in 
number = int(input("How many times should it be printed? "))
ValueError: invalid literal for int() with base 10: 'How many times should
it be printed? 5'


That is exactly how it looks like. It is happening all the time when I run
program in Komodo Edit, but in IDLE everything works fine (interpreter is
the same):


Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]
on win32
Type "copyright", "credits" or "license()" for more information.
>>> text = str(input("Type in some text: "))
Type in some text: some
>>> number = int(input("How many times should it be printed? "))
How many times should it be printed? 5
>>> print (text * number)
somesomesomesomesome
>>>

For your suggestion

Code:
text = str(input("Type in some text: "))
snumber = input("How many times should it be printed?")
try:
 number = int(snumber)
except ValueError:
 print(ssnumber, "is not an integer.")
else:
 print (text * snumber)



Output:
Type in some text: some
How many times should it be printed?5
How many times should it be printed?5 is not an integer.

On Mon, Feb 15, 2010 at 4:06 PM, bob gailer  wrote:

> Yaraslau Shanhin wrote:
>
> > Hello All,
>
> Hello.
>
> Suggestion for future questions - just include enough to identify the
> problem. We don't need to know that this is from a tutorial or what the
> exercise is. Also (at least I prefer) plain text without formatting or
> color.
>
> Sufficient therefore is:
>
> ---
> using  Python 3.1:
>
> text = str(input("Type in some text: "))
> number = int(input("How many times should it be printed? "))
>
> Traceback (most recent call last):
> File "test4.py", line 2, in 
>   number = int(input("How many times should it be printed? "))
> ValueError: invalid literal for int() with base 10:
> ---
>
> Now carefully read the message "invalid literal for int()". This tells you
> the argument to int is not a string representation of an integer. There is
> nothing wrong with input()!
>
> Since we don't know what you typed at the input() prompt we can't diagnose
> it further. One way to solve problems like these is to capture and print the
> output of input():
>
>
> text = str(input("Type in some text: "))
> snumber = input("How many times should it be printed?")
> print snumber
>
> A good robust alternative is to try int() and capture / report the failure:
>
>
> text = str(input("Type in some text: "))
> snumber = input("How many times should it be printed?")
> try:
>  number = int(snumber)
> except ValueError:
>  print(number, "is not an integer.")
> else:
>  print (text * number)
>
> --
> 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] Input() is not working as expected in Python 3.1

2010-02-15 Thread Rich Lovely
On 15 February 2010 15:15, Yaraslau Shanhin  wrote:
> Code:
> text = str(input("Type in some text: "))
> number = int(input("How many times should it be printed? "))
> print (text * number)
> Output:
> Type in some text: some
> How many times should it be printed? 5
> Traceback (most recent call last):
>   File "test4.py", line 2, in 
>     number = int(input("How many times should it be printed? "))
> ValueError: invalid literal for int() with base 10: 'How many times should
> it be printed? 5'
>
> That is exactly how it looks like. It is happening all the time when I run
> program in Komodo Edit, but in IDLE everything works fine (interpreter is
> the same):
>
> Python 3.1.1 (r311:74483, Aug 17 2009, 17:02:12) [MSC v.1500 32 bit (Intel)]
> on win32
> Type "copyright", "credits" or "license()" for more information.
 text = str(input("Type in some text: "))
> Type in some text: some
 number = int(input("How many times should it be printed? "))
> How many times should it be printed? 5
 print (text * number)
> somesomesomesomesome

> For your suggestion
> Code:
> text = str(input("Type in some text: "))
> snumber = input("How many times should it be printed?")
> try:
>  number = int(snumber)
> except ValueError:
>  print(ssnumber, "is not an integer.")
> else:
>  print (text * snumber)
>
>
> Output:
> Type in some text: some
> How many times should it be printed?5
> How many times should it be printed?5 is not an integer.
> On Mon, Feb 15, 2010 at 4:06 PM, bob gailer  wrote:
>>
>> Yaraslau Shanhin wrote:
>>
>> > Hello All,
>>
>> Hello.
>>
>> Suggestion for future questions - just include enough to identify the
>> problem. We don't need to know that this is from a tutorial or what the
>> exercise is. Also (at least I prefer) plain text without formatting or
>> color.
>>
>> Sufficient therefore is:
>>
>> ---
>> using  Python 3.1:
>> text = str(input("Type in some text: "))
>> number = int(input("How many times should it be printed? "))
>>
>> Traceback (most recent call last):
>> File "test4.py", line 2, in 
>>   number = int(input("How many times should it be printed? "))
>> ValueError: invalid literal for int() with base 10:
>> ---
>>
>> Now carefully read the message "invalid literal for int()". This tells you
>> the argument to int is not a string representation of an integer. There is
>> nothing wrong with input()!
>>
>> Since we don't know what you typed at the input() prompt we can't diagnose
>> it further. One way to solve problems like these is to capture and print the
>> output of input():
>>
>> text = str(input("Type in some text: "))
>> snumber = input("How many times should it be printed?")
>> print snumber
>>
>> A good robust alternative is to try int() and capture / report the
>> failure:
>>
>> text = str(input("Type in some text: "))
>> snumber = input("How many times should it be printed?")
>> try:
>>  number = int(snumber)
>> except ValueError:
>>  print(number, "is not an integer.")
>> else:
>>  print (text * number)
>>
>> --
>> Bob Gailer
>> 919-636-4239
>> Chapel Hill NC
>>
>
>
> ___
> Tutor maillist  -  tu...@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
This is a known bug with Komodo (see
http://bugs.activestate.com/show_bug.cgi?id=71319).
The developers are working on it, by the looks of things.

-- 
Rich "Roadie Rich" Lovely

Just because you CAN do something, doesn't necessarily mean you SHOULD.
In fact, more often than not, you probably SHOULDN'T.  Especially if I
suggested it.

10 re-discover BASIC
20 ???
30 PRINT "Profit"
40 GOTO 10
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Editing html using python

2010-02-15 Thread Serdar Tumgoren
In the few cases I had where BeautifulSoup couldn't handle poorly formed
HTML, I've found that html5lib was able to get the job done. And of course,
lxml is great too, but has a bit more overhead installation-wis.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Getting caller name without the help of "sys._getframe(1).f_code.co_name" ?

2010-02-15 Thread patrice laporte
2010/2/14 Steven D'Aprano 

>
> > If the file do esn't exist, bing ! I got an exception "I/O Error n°2
> > : file doesn't exist".
>
> Are you sure? What version of Python are you using? I get a completely
> different error message:
>
>
Yes I'm not sure  when I wrote : *I got an exception "I/O Error n°2 :
file doesn't exist".*
It was just to stress the point, it was not the real message.

>>> open("no such file.txt", "r")
> Traceback (most recent call last):
>   File "", line 1, in 
> IOError: [Errno 2] No such file or directory: 'no such file.txt'
>

 And of course,  If I do this, I got the same traceback, and I can see the
file name.


> >>> try:
> ... open("no such file.txt", "r")
> ... except IOError, e:
> ... pass
> ...
> >>> e.filename
> 'no such file.txt'
>
>
 This is it, this way to catch the exception what I wanted. Reading the
documentation and articles over the web, I found that :

try:
   f = open("bling")
except IOError, (errno, errmes):
   # that way, I can only print the erreor number and the message
   # but the message doesn't contain the file name, and it confused me
because traceback can
   # give it. So where to find it ?

But with your example, I can access all attribute I can read doing >>*help(e)
*in the interpreter*
*
With documentation, I understood filename is an attribute, but I didn't
understand how to use it. The part of the documentation about 'buil-tins
Exception' and the other part about "how to handle exceptin" didn't help me,
there is something I missed about how to handle exception.

Thank you for this help, I will look closer again to exception.

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


Re: [Tutor] Input() is not working as expected in Python 3.1

2010-02-15 Thread Grigor Kolev
В 10:06 -0500 на 15.02.2010 (пн), bob gailer написа:
> Yaraslau Shanhin wrote:
> 
>  > Hello All,
> 
> Hello.
> 
> Suggestion for future questions - just include enough to identify the 
> problem. We don't need to know that this is from a tutorial or what the 
> exercise is. Also (at least I prefer) plain text without formatting or 
> color.
> 
> Sufficient therefore is:
> 
> ---
> using  Python 3.1:
> text = str(input("Type in some text: "))
> number = int(input("How many times should it be printed? "))
> 
> Traceback (most recent call last):
> File "test4.py", line 2, in 
> number = int(input("How many times should it be printed? "))
> ValueError: invalid literal for int() with base 10:
> ---
> 
> Now carefully read the message "invalid literal for int()". This tells 
> you the argument to int is not a string representation of an integer. 
> There is nothing wrong with input()!
> 
> Since we don't know what you typed at the input() prompt we can't 
> diagnose it further. One way to solve problems like these is to capture 
> and print the output of input():
> 
> text = str(input("Type in some text: "))
> snumber = input("How many times should it be printed?")
> print snumber
> 
> A good robust alternative is to try int() and capture / report the failure:
> 
> text = str(input("Type in some text: "))
> snumber = input("How many times should it be printed?")
> try:
>   number = int(snumber)
> except ValueError:
>   print(number, "is not an integer.")
> else:
>   print (text * number)
> 
Try this:
>>>text = str(input("Type in some text: "))
Type in some text:"some text"
>>>snumber = int(input("How many times should it be printed?"))
How many times should it be printed?3
>>>print text*snumber
-- 
Grigor Kolev 

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


Re: [Tutor] A Stuborn Tab Problem in IDLE

2010-02-15 Thread Wayne Watson
I've found there's quite a bit of discrepancy in top vs bottom posting. 
It's hardly worth thinking about. I seem to navigate through top, bottom 
or mixed. The real problem, IMHO, is very long posts from various people.


Marty


Ah ha! Sorry for the noise, I should really read the whole message
before pressing send. :-D Glad you fixed the problem.


   


--
"Crime is way down. War is declining. And that's far from the good 
news." -- Steven Pinker (and other sources) Why is this true, but yet 
the media says otherwise? The media knows very well how to manipulate us 
(see limbic, emotion, $$). -- WTW

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread pja
Here is an interesting solution that I developed using the EasyGUI 
(http://easygui.sourceforge.net/) graphics library:



# 7.2_quadratic2easy.py
# A program that computes the real roots of a quadratic equation.
# Bad version using a simple if to avoid program crash
# This version uses the EasyGUI graphics library
# See http://easygui.sourceforge.net/ for EasyGUI details

import math
from easygui import *

def main():

msg = "This program finds the real solutions to a quadratic" + "\n" + \
"Please enter the three coefficients below."
title = "quadratic2easy.py"
fieldNames = ["First coefficient","Second coefficient","Third coefficient"]
fieldValues = [] # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)

# make sure that none of the fields was left blank
while 1:
if fieldValues == None: break
errmsg = ""
for i in range(len(fieldNames)):
if fieldValues[i].strip() == "":
errmsg = errmsg + ('"%s" is a required field.' % fieldNames[i])
if errmsg == "": break # no problems found
fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)

print("Reply was:", fieldValues)

a = int(fieldValues[0])
b = int(fieldValues[1])
c = int(fieldValues[2])

discrim = a * a - 4 * b * c
if discrim >= 0:
discRoot = math.sqrt(discrim)
root1 = (-a + discRoot) / (2 * b)
root2 = (-a - discRoot) / (2 * b)
message = "The real solutions to the quadratic equation are " + 
str(root1) + " and " + str(root2)

msgbox(msg=message, title="quadratic2easy.py",
ok_button="OK", image=None)

main()


Thanks to all those who provided advice; this is a great resource for us 
learners.


Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6

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


Re: [Tutor] Problem with "input" in Python 3

2010-02-15 Thread pja
Sorry about the previous post; I didn't check the code format before I 
posted the reply and thus lost all the indentation. Here I go again, 
hopefully with the correct indentation.



# 7.2_quadratic2easy.py
# A program that computes the real roots of a quadratic equation.
# Bad version using a simple if to avoid program crash
# This version uses the EasyGUI graphics library
# See http://easygui.sourceforge.net/ for EasyGUI details

import math
from easygui import *

def main():

msg = "This program finds the real solutions to a quadratic" + "\n" + \
"Please enter the three coefficients below."
title = "quadratic2easy.py"
fieldNames = ["First coefficient","Second coefficient","Third coefficient"]
fieldValues = [] # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)

# make sure that none of the fields was left blank
while 1:
if fieldValues == None: break
errmsg = ""
for i in range(len(fieldNames)):
if fieldValues[i].strip() == "":
errmsg = errmsg + ('"%s" is a required field.' % fieldNames[i])
if errmsg == "": break # no problems found
fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)

print("Reply was:", fieldValues)

a = int(fieldValues[0])
b = int(fieldValues[1])
c = int(fieldValues[2])

discrim = a * a - 4 * b * c
if discrim >= 0:
discRoot = math.sqrt(discrim)
root1 = (-a + discRoot) / (2 * b)
root2 = (-a - discRoot) / (2 * b)
message = "The real solutions to the quadratic equation are " + 
str(root1) + " and " + str(root2)

msgbox(msg=message, title="quadratic2easy.py",
ok_button="OK", image=None)

main()


Regards,
Peter
--
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6

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


[Tutor] Problem with "input" in Python 3

2010-02-15 Thread Peter Anderson
I seem to be having a formatting problem between Thunderbird on my PC
and the mailing list; I am loosing all indentation. The Gmail account
settings on Thunderbird are for plain text and I set the e-mail
content to fixed width text so I'm not sure what's going wrong. I am
trying one last time sending from Gmail (as fixed text).


#   7.2_quadratic2easy.py
#   A program that computes the real roots of a quadratic equation.
#   Bad version using a simple if to avoid program crash
#   This version uses the EasyGUI graphics library
#   See http://easygui.sourceforge.net/ for EasyGUI details

import math
from easygui import *

def main():

msg = "This program finds the real solutions to a quadratic" + "\n" + \
"Please enter the three coefficients below."
title = "quadratic2easy.py"
fieldNames = ["First coefficient","Second coefficient","Third coefficient"]
fieldValues = []  # we start with blanks for the values
fieldValues = multenterbox(msg,title, fieldNames)

# make sure that none of the fields was left blank
while 1:
if fieldValues == None: break
errmsg = ""
for i in range(len(fieldNames)):
if fieldValues[i].strip() == "":
errmsg = errmsg + ('"%s" is a required field.' % fieldNames[i])
if errmsg == "": break # no problems found
fieldValues = multenterbox(errmsg, title, fieldNames, fieldValues)

print("Reply was:", fieldValues)

a = int(fieldValues[0])
b = int(fieldValues[1])
c = int(fieldValues[2])

discrim = a * a - 4 * b * c
if discrim >= 0:
discRoot = math.sqrt(discrim)
root1 = (-a + discRoot) / (2 * b)
root2 = (-a - discRoot) / (2 * b)
message  = "The real solutions to the quadratic equation are "
+ str(root1) + " and " + str(root2)
msgbox(msg=message, title="quadratic2easy.py",
ok_button="OK", image=None)

main()


--
There is nothing more difficult to take in hand, more perilous to
conduct, or more uncertain in its success, than to take the lead in
the introduction of a new order of things -- Niccolo Machiavelli, The
Prince, ch. 6
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Input() is not working as expected in Python 3.1

2010-02-15 Thread Yaraslau Shanhin
Thanks for try but print without () does not work at all, at least in newer
version of python (3.1)

Anyway source of problem is now discovered: Komodo Edit tool, in Python
shell (IDLE) everything works fine. Perhaps anyone has any ideas why?

On Mon, Feb 15, 2010 at 10:02 PM, Grigor Kolev wrote:

> В 10:06 -0500 на 15.02.2010 (пн), bob gailer написа:
> > Yaraslau Shanhin wrote:
> >
> >  > Hello All,
> >
> > Hello.
> >
> > Suggestion for future questions - just include enough to identify the
> > problem. We don't need to know that this is from a tutorial or what the
> > exercise is. Also (at least I prefer) plain text without formatting or
> > color.
> >
> > Sufficient therefore is:
> >
> > ---
> > using  Python 3.1:
> > text = str(input("Type in some text: "))
> > number = int(input("How many times should it be printed? "))
> >
> > Traceback (most recent call last):
> > File "test4.py", line 2, in 
> > number = int(input("How many times should it be printed? "))
> > ValueError: invalid literal for int() with base 10:
> > ---
> >
> > Now carefully read the message "invalid literal for int()". This tells
> > you the argument to int is not a string representation of an integer.
> > There is nothing wrong with input()!
> >
> > Since we don't know what you typed at the input() prompt we can't
> > diagnose it further. One way to solve problems like these is to capture
> > and print the output of input():
> >
> > text = str(input("Type in some text: "))
> > snumber = input("How many times should it be printed?")
> > print snumber
> >
> > A good robust alternative is to try int() and capture / report the
> failure:
> >
> > text = str(input("Type in some text: "))
> > snumber = input("How many times should it be printed?")
> > try:
> >   number = int(snumber)
> > except ValueError:
> >   print(number, "is not an integer.")
> > else:
> >   print (text * number)
> >
> Try this:
> >>>text = str(input("Type in some text: "))
> Type in some text:"some text"
> >>>snumber = int(input("How many times should it be printed?"))
> How many times should it be printed?3
> >>>print text*snumber
> --
> Grigor Kolev 
>
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor