Re: [Tutor] Bug

2017-05-17 Thread Cameron Simpson

On 17May2017 12:26, Grace Sanford  wrote:

Theoretically, the following code is suppose to check if the user has won a
tic tac toe game by checking if there are all "X"s in either the
horizontal, vertical, or diagonal lines of a grid (represented by a list
with "board" with elements 0-8).  If one of these is the case, it is
suppose to print the "You won" string.  Nevertheless, when I change list
variable to reflect one of these conditions, there is no printing
occurring.  I cannot figure out why.

if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or
board[6:9]==["X", "X", "X"] or \
   [board[0],board[3],board[6]]==["X", "X", "X"] or
[board[1],board[4],board[7]]==["X", "X", "X"] or
[board[2],board[5],board[8]] ==["X", "X", "X"] or \
   [board[0],board[4],board[8]]==["X", "X", "X"] or
[board[2],board[4],board[6]]==["X", "X", "X"]:


Please post complete code, and the output (I accept that in your case the 
output is empty). For example:


 board = [ "X", "X", "X",
   "", "", "",
   "", "", ""
 ]
 if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or board[6:9]==["X", "X", 
"X"] or \
[board[0],board[3],board[6]]==["X", "X", "X"] or [board[1],board[4],board[7]]==["X", "X", "X"] or 
[board[2],board[5],board[8]] ==["X", "X", "X"] or \
[board[0],board[4],board[8]]==["X", "X", "X"] or [board[2],board[4],board[6]]==["X", 
"X", "X"]:
  print("ROW!")

so that proeple can reproduce your problem. For example, it may be that some 
winning positions do work and some don't, and you've tested only a failing 
combination.


The example I have above prints "ROW!" for me, and it is just your own code 
with a specific combination.


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


Re: [Tutor] Bug

2017-05-17 Thread Alan Gauld via Tutor
On 17/05/17 17:26, Grace Sanford wrote:

> with "board" with elements 0-8).  If one of these is the case, it is
> suppose to print the "You won" string.  Nevertheless, when I change list
> variable to reflect one of these conditions, there is no printing
> occurring.  I cannot figure out why.

You need to show us the whole code including the print statements.
Also, do you get an error message? If so please include the full message.

As it stands the formatting is all messed up but unless you have it all
on one line (or 3 lines, I just noticed the \ chars...) I suspect you
will get a syntax error?

To fix that you can put parentheses round the expression:


if ( board[0:3]==["X", "X", "X"] or
 board[3:6]==["X", "X", "X"] or
 board[6:9]==["X", "X", "X"] or
 [board[0],board[3],board[6]]==["X", "X", "X"] or
 [board[1],board[4],board[7]]==["X", "X", "X"] or
 [board[2],board[5],board[8]]==["X", "X", "X"] or
 [board[0],board[4],board[8]]==["X", "X", "X"] or
 [board[2],board[4],board[6]]==["X", "X", "X"]
   ):

Have you tried breaking it down in the interpreter?
Try an if statement with just the first three lines?
Then the last two? etc That will help identify where
the problem lies.

At a casual glance I can't see any issues with the
code above.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


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


[Tutor] Bug

2017-05-17 Thread Grace Sanford
Theoretically, the following code is suppose to check if the user has won a
tic tac toe game by checking if there are all "X"s in either the
horizontal, vertical, or diagonal lines of a grid (represented by a list
with "board" with elements 0-8).  If one of these is the case, it is
suppose to print the "You won" string.  Nevertheless, when I change list
variable to reflect one of these conditions, there is no printing
occurring.  I cannot figure out why.

if board[0:3]==["X", "X", "X"] or board[3:6]==["X", "X", "X"] or
board[6:9]==["X", "X", "X"] or \
[board[0],board[3],board[6]]==["X", "X", "X"] or
[board[1],board[4],board[7]]==["X", "X", "X"] or
[board[2],board[5],board[8]] ==["X", "X", "X"] or \
[board[0],board[4],board[8]]==["X", "X", "X"] or
[board[2],board[4],board[6]]==["X", "X", "X"]:
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] bug in exam score conversion program

2008-10-04 Thread David

Hello!!

I just completed exercise 7 (chapter 4) in Zelle's book:
A certain CS professor gives 100-point exams that are graded on the 
scale 90–100:A, 80–89:B, 70–79:C, 60–69:D, 60:F. Write a program that 
accepts an exam score as input and prints out the corresponding grade.


I am quite happy with my code, but there is a bug: if the score is 100, 
then the program calculates 100/10 = 10. However, the tuple runs to 9, 
leaving me with an error message: IndexError: tuple index out of range


I can't figure out how to solve that problem...
I also suspect that my code clearly exposes me as a beginner :-) What 
would be the pythonic way of solving that exercise?


# exam score to grade conversion
# Zelle, ch. 4, exercise 7
x = (F, F, F, F, F, E, D, C, B, A)
score = raw_input(What's your exam score (0-100)? )
grade = x[int(score)/10]
print Your grade is:, grade

I greatly appreciate your comments!

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Sander Sweers
On Sat, Oct 4, 2008 at 12:11, David [EMAIL PROTECTED] wrote:
 I am quite happy with my code, but there is a bug: if the score is 100, then
 the program calculates 100/10 = 10. However, the tuple runs to 9, leaving me
 with an error message: IndexError: tuple index out of range

 I can't figure out how to solve that problem...
 I also suspect that my code clearly exposes me as a beginner :-) What would
 be the pythonic way of solving that exercise?

 # exam score to grade conversion
 # Zelle, ch. 4, exercise 7
 x = (F, F, F, F, F, E, D, C, B, A)
 score = raw_input(What's your exam score (0-100)? )
 grade = x[int(score)/10]
 print Your grade is:, grade

Python starts counting from zero so to access the first item in x it
would be x[0]. When you do x[10] you actually are requestting the 11th
value in x instead of the 10th and this does not exist.

You should be able to fix this yourself now.

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread W W
On Sat, Oct 4, 2008 at 5:11 AM, David [EMAIL PROTECTED] wrote:

 Hello!!

snip

I can't figure out how to solve that problem...
 I also suspect that my code clearly exposes me as a beginner :-) What would
 be the pythonic way of solving that exercise?

 # exam score to grade conversion
 # Zelle, ch. 4, exercise 7
 x = (F, F, F, F, F, E, D, C, B, A)
 score = raw_input(What's your exam score (0-100)? )
 grade = x[int(score)/10]
 print Your grade is:, grade


Wow! That's actually a really surprising way of solving this, and not
entirely bad. The easiest way of modifying this program is to do exactly
what you did with the Fs, only with an A.

I suspect most people would have created an if else chain:

if score =  90:
grade = A
elif score = 80:
grade = B
elif score = 70:
grade = C
elif score = 60:
grade = D
else:
grade = F

print Your grade is %s % (grade, )

But in reality, there's nothing wrong with your method - as a matter of
fact, it's probably slightly more optimized (although we're talking in time
frames of  .1 seconds) because rather than making comparisons it makes
one computation and then directly accesses the values stored in the tuple.

So, I'll compliment you for solving a problem in a way that I never would
have thought of, helping me to think outside the box! In addition a tuple is
an ideal variable here because it's constant - you cannot change the values
or size of a tuple, unlike a list or dictionary. Not that (at this point)
you're worried about malicious type users, but  it's always a good idea to
fool proof your code.

Well, I hope this helps both look at your problem a different way, as well
as solves your bug (in this case, a syntax error: the code behaves exactly
how it should, you just made an error in how you implemented your code.
Those will be your most common bugs, from here until forever ;) )
-Wayne
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld

David [EMAIL PROTECTED] wrote

I am quite happy with my code, but there is a bug: if the score is 
100, then the program calculates 100/10 = 10. However, the tuple 
runs to 9, leaving me with an error message: IndexError: tuple index 
out of range


I can't figure out how to solve that problem...


I also suspect that my code clearly exposes me as a beginner :-) 
What would be the pythonic way of solving that exercise?


# exam score to grade conversion
# Zelle, ch. 4, exercise 7
x = (F, F, F, F, F, E, D, C, B, A)
score = raw_input(What's your exam score (0-100)? )
grade = x[int(score)/10]
print Your grade is:, grade


It's not too bad but I would probably use a dictionary rather
than the list - which avoids the index problem - and I'd do
the int conversion with raw_input::

Grades = {0:'F', 1:'F',2:'F',8:'B', 9:'A',10:'A'}
score = int(raw_input(What's your exam score (0-100)? ))
print Your grade is:, Grades[score/10]

HTH,

Alan G





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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Brian C. Lane
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

David wrote:
 Hello!!
 
 I just completed exercise 7 (chapter 4) in Zelle's book:
 A certain CS professor gives 100-point exams that are graded on the
 scale 90–100:A, 80–89:B, 70–79:C, 60–69:D, 60:F. Write a program that
 accepts an exam score as input and prints out the corresponding grade.
 

Just to throw in another method, I tend to use tables of for problems
like this. The requirements usually change so its easier to modify later:

# min, max, grade
grades = [  (90,100,'A'),
(80, 89,'B'),
(70, 79,'C'),
(60, 69,'D'),
( 0, 59,'F'),
]

def getGrade(score):

Return a letter grade based on a score

for g in grades:
if (score = g[1]) and (score = g[0]):
return g[2]



- --
- ---[Office 71.6F]--[Outside 55.4F]--[Server 107.9F]--[Coaster 71.7F]---
- ---[   WSF KITSAP (366772980) @ 47 34.7811 -122 27.7554   ]---
Software, Linux, Microcontrollers http://www.brianlane.com
AIS Parser SDKhttp://www.aisparser.com

-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.8 (Darwin)
Comment: Remember Lexington Green!

iD8DBQFI535RIftj/pcSws0RAldqAJ9yKYSyDArc/LZ6G47SwxUq4z8yAACgioyx
b9WnwDEQe8hSOuYbKuKo9sY=
=7lCV
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld


Brian C. Lane [EMAIL PROTECTED] wrote


# min, max, grade
grades = [  (90,100,'A'),
   (80, 89,'B'),
   (70, 79,'C'),
   (60, 69,'D'),
   ( 0, 59,'F'),
   ]

def getGrade(score):
   
   Return a letter grade based on a score
   
   for g in grades:
   if (score = g[1]) and (score = g[0]):
   return g[2]


Could be written more concisely as

for g in grades:
   if g[0] = score = g[1]:
   return g[2]




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










- --
- ---[Office 71.6F]--[Outside 55.4F]--[Server 107.9F]--[Coaster 
71.7F]---
- ---[   WSF KITSAP (366772980) @ 47 34.7811 -122 
4   ]---
Software, Linux, Microcontrollers 
http://www.brianlane.com
AIS Parser SDK 
http://www.aisparser.com


-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.8 (Darwin)
Comment: Remember Lexington Green!

iD8DBQFI535RIftj/pcSws0RAldqAJ9yKYSyDArc/LZ6G47SwxUq4z8yAACgioyx
b9WnwDEQe8hSOuYbKuKo9sY=
=7lCV
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor




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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Kent Johnson
On Sat, Oct 4, 2008 at 10:31 AM, Brian C. Lane [EMAIL PROTECTED] wrote:

for g in grades:
if (score = g[1]) and (score = g[0]):
return g[2]

I think tuple unpacking makes code like this more readable:

for lower, upper, grade in grades:
  if lower = score = upper:
return grade

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Kent Johnson
On Sat, Oct 4, 2008 at 9:45 AM, Alan Gauld [EMAIL PROTECTED] wrote:

 It's not too bad but I would probably use a dictionary rather
 than the list - which avoids the index problem

Not sure how the dict is better - in either case, leaving off the
grade corresponding to a score of 100 will raise an exception.

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread David
When I run it from the idle it works perfect, but when I run it from a
file I get none, why is that?

 grades = [  (90,100,'A'),
(80, 89,'B'),
(70, 79,'C'),
(60, 69,'D'),
( 0, 59,'F'),
]

 score = 66
 def getGrade(score):

Return a letter grade based on a score

for g in grades:
if (score = g[1]) and (score = g[0]):
return g[2]

 getGrade
function getGrade at 0x84ec80
 getGrade(score)
'D'


#!/usr/bin/python

grades = [  (90,100,'A'),
(80, 89,'B'),
(70, 79,'C'),
(60, 69,'D'),
( 0, 59,'F'),
]

def getGrade(score):

Return a letter grade based on a score

for g in grades:
if (score = g[1]) and (score = g[0]):
return g[2]

score = raw_input(What is your exam score: (0-100)? )
print getGrade
print getGrade(score)

What is your exam score: (0-100)? 66
function getGrade at 0x2b4b2a310d70
None

-- 
Have Fun,
David A.

Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Kent Johnson
On Sat, Oct 4, 2008 at 12:55 PM, David [EMAIL PROTECTED] wrote:
 When I run it from the idle it works perfect, but when I run it from a
 file I get none, why is that?
 score = raw_input(What is your exam score: (0-100)? )

The value returned from raw_input() is a string; you have to convert
it to an int:
score = int(raw_input(What is your exam score: (0-100)? ))

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Dragos Ionescu
David try this:
 
score = input(What is your exam score: (0-100)? )
print getGrade
print getGrade(score)


Regards,
Dragoshttp://scripts.mit.edu/~dionescu/pyworld/ 



- Original Message 
From: David [EMAIL PROTECTED]
To: Brian C. Lane [EMAIL PROTECTED]
Cc: tutor@python.org
Sent: Saturday, October 4, 2008 7:55:57 PM
Subject: Re: [Tutor] bug in exam score conversion program

When I run it from the idle it works perfect, but when I run it from a
file I get none, why is that?

 grades = [  (90,100,'A'),
            (80, 89,'B'),
            (70, 79,'C'),
            (60, 69,'D'),
            ( 0, 59,'F'),
        ]

 score = 66
 def getGrade(score):
    
    Return a letter grade based on a score
    
    for g in grades:
        if (score = g[1]) and (score = g[0]):
            return g[2]

 getGrade
function getGrade at 0x84ec80
 getGrade(score)
'D'


#!/usr/bin/python

grades = [  (90,100,'A'),
            (80, 89,'B'),
            (70, 79,'C'),
            (60, 69,'D'),
            ( 0, 59,'F'),
        ]

def getGrade(score):
    
    Return a letter grade based on a score
    
    for g in grades:
        if (score = g[1]) and (score = g[0]):
            return g[2]

score = raw_input(What is your exam score: (0-100)? )
print getGrade
print getGrade(score)

What is your exam score: (0-100)? 66
function getGrade at 0x2b4b2a310d70
None

-- 
Have Fun,
David A.

Powered by Gentoo GNU/LINUX
http://www.linuxcrazy.com

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



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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread bob gailer

Lots of good responses. And now for something completely different:

import string
x = string.maketrans('567891', 'FDCBAA')
score = raw_input('score')
print Your grade is:, score[0].translate(x)

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


When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.


Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?

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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Dragos Ionescu
- Original Message 
From: bob gailer [EMAIL PROTECTED]
To: David [EMAIL PROTECTED]
Cc: tutor@python.org
Sent: Saturday, October 4, 2008 10:15:10 PM
Subject: Re: [Tutor] bug in exam score conversion program

Lots of good responses. And now for something completely different:

import string
x = string.maketrans('567891', 'FDCBAA')
score = raw_input('score')
print Your grade is:, score[0].translate(x)

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

When we take the time to be aware of our feelings and 
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?

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



Wow! Bob Gailer's solution is so elegant. Can someone plese explain what is the 
algorithm behind  string.maketrans. More exactly, how is this function doing 
the coding? 
 http://scripts.mit.edu/~dionescu/pyworld/



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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Steve Willoughby

Dragos Ionescu wrote:

- Original Message 
From: bob gailer [EMAIL PROTECTED]
To: David [EMAIL PROTECTED]
Cc: tutor@python.org
Sent: Saturday, October 4, 2008 10:15:10 PM
Subject: Re: [Tutor] bug in exam score conversion program

Lots of good responses. And now for something completely different:

import string
x = string.maketrans('567891', 'FDCBAA')
score = raw_input('score')
print Your grade is:, score[0].translate(x)
--
Bob Gailer
Chapel Hill NC
919-636-4239

When we take the time to be aware of our feelings and
needs we have more satisfying interatctions with others.

Nonviolent Communication provides tools for this awareness.

As a coach and trainer I can assist you in learning this process.

What is YOUR biggest relationship challenge?

___
Tutor maillist  -  Tutor@python.org mailto:Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
 
 
Wow! Bob Gailer's solution is so elegant. Can someone plese explain what 
is the algorithm behind  string.maketrans. More exactly, how is this 
function doing the coding?


Actually, I don't think the point was to be elegant as much
as to get you thinking about something you might not have
explored--never hurts to keep learning new features so you
don't inefficiently apply the same old small set of things
to new problems.

You wouldn't *really* want to implement a production grade
system like that, cute though it is.  This is setting up a
translation table mapping the first character in the score
to a letter grade.  So a 9 is changed to an A.  The obvious
problem though is how it handles a score of, say, 1.  Or,
for that matter, 37.



 http://scripts.mit.edu/~dionescu/pyworld/




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


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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Dragos Ionescu
 Original Message 
From: Steve Willoughby [EMAIL PROTECTED]
To: Dragos Ionescu [EMAIL PROTECTED]
Cc: bob gailer [EMAIL PROTECTED]; David [EMAIL PROTECTED]; tutor@python.org
Sent: Saturday, October 4, 2008 11:04:30 PM
Subject: Re: [Tutor] bug in exam score conversion program

Dragos Ionescu wrote:
 - Original Message 
 From: bob gailer [EMAIL PROTECTED]
 To: David [EMAIL PROTECTED]
 Cc: tutor@python.org
 Sent: Saturday, October 4, 2008 10:15:10 PM
 Subject: Re: [Tutor] bug in exam score conversion program
 
 Lots of good responses. And now for something completely different:
 
 import string
 x = string.maketrans('567891', 'FDCBAA')
 score = raw_input('score')
 print Your grade is:, score[0].translate(x)
 -- 
 Bob Gailer
 Chapel Hill NC
 919-636-4239
 
 When we take the time to be aware of our feelings and
 needs we have more satisfying interatctions with others..
 
 Nonviolent Communication provides tools for this awareness.
 
 As a coach and trainer I can assist you in learning this process.
 
 What is YOUR biggest relationship challenge?
 
 ___
 Tutor maillist  -  Tutor@python.org mailto:Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
  
  
 Wow! Bob Gailer's solution is so elegant. Can someone plese explain what 
 is the algorithm behind  string.maketrans. More exactly, how is this 
 function doing the coding?

Actually, I don't think the point was to be elegant as much
as to get you thinking about something you might not have
explored--never hurts to keep learning new features so you
don't inefficiently apply the same old small set of things
to new problems.

You wouldn't *really* want to implement a production grade
system like that, cute though it is.  This is setting up a
translation table mapping the first character in the score
to a letter grade.  So a 9 is changed to an A.  The obvious
problem though is how it handles a score of, say, 1.  Or,
for that matter, 37.


I know how string.maketrans works. I was wondering how to implement such a 
function. Would that be very hard? I must admit that I was 'surprised' when I 
printed x...

Thanks,
Dragos
http://scripts.mit.edu/~dionescu/pyworld


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


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Steve Willoughby

Dragos Ionescu wrote:

 Original Message 
From: Steve Willoughby [EMAIL PROTECTED]
To: Dragos Ionescu [EMAIL PROTECTED]
Cc: bob gailer [EMAIL PROTECTED]; David [EMAIL PROTECTED]; tutor@python.org
Sent: Saturday, October 4, 2008 11:04:30 PM
Subject: Re: [Tutor] bug in exam score conversion program

Dragos Ionescu wrote:
  - Original Message 
  From: bob gailer [EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
  To: David [EMAIL PROTECTED] mailto:[EMAIL PROTECTED]
  Cc: tutor@python.org mailto:tutor@python.org
  Sent: Saturday, October 4, 2008 10:15:10 PM
  Subject: Re: [Tutor] bug in exam score conversion program
 
  Lots of good responses. And now for something completely different:
 
  import string
  x = string.maketrans('567891', 'FDCBAA')
  score = raw_input('score')
  print Your grade is:, score[0].translate(x)
  --
  Bob Gailer
  Chapel Hill NC
  919-636-4239
 
  When we take the time to be aware of our feelings and
  needs we have more satisfying interatctions with others.
 
  Nonviolent Communication provides tools for this awareness.
 
  As a coach and trainer I can assist you in learning this process.
 
  What is YOUR biggest relationship challenge?
 
  ___
  Tutor maillist  -  Tutor@python.org mailto:Tutor@python.org 
mailto:Tutor@python.org mailto:Tutor@python.org

  http://mail.python.org/mailman/listinfo/tutor
  
  
  Wow! Bob Gailer's solution is so elegant. Can someone plese explain what

  is the algorithm behind  string.maketrans. More exactly, how is this
  function doing the coding?

Actually, I don't think the point was to be elegant as much
as to get you thinking about something you might not have
explored--never hurts to keep learning new features so you
don't inefficiently apply the same old small set of things
to new problems.

You wouldn't *really* want to implement a production grade
system like that, cute though it is.  This is setting up a
translation table mapping the first character in the score
to a letter grade.  So a 9 is changed to an A.  The obvious
problem though is how it handles a score of, say, 1.  Or,
for that matter, 37.


I know how string.maketrans works. I was wondering how to implement such 
a function. Would that be very hard? I must admit that I was 'surprised' 
when I printed x...


How to implement... the equivalent of maketrans/translate?  Pretty
easy really.  maketrans just builds a 256-byte table showing a mapping
from one character set to another (compare perl's y/// or tr///).  Once 
you have that translation table, all you really need to do is take each 
character of a string and make a new string by looking up each source 
character and returning what the table says (effectively table[ord(i)] 
for each character i in the source string).  Which is pretty much

what string.translate() is doing.

or did I misunderstand which function you wanted to implement?
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] bug in exam score conversion program

2008-10-04 Thread Alan Gauld


David [EMAIL PROTECTED] wrote

When I run it from the idle it works perfect, but when I run it from 
a

file I get none, why is that?



score = 66


Here you directly assign a number to score


#!/usr/bin/python

score = raw_input(What is your exam score: (0-100)? )


Here you assign a string - the result of raw_input


print getGrade(score)


But getGrade expects to get an integer so yo need to
convert score to an int either when you pass it to getGrade
or, more usually, the return value from raw_input.

Passing a string to getGrade means it never finds a match
so never returns a grade and instead falls off the bottom with
no specified return value. When this happens Python inserts
a default return value of None

HTH,

Alan G 



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


Re: [Tutor] bug or feature

2007-06-01 Thread Andreas Kostyrka
-BEGIN PGP SIGNED MESSAGE-
Hash: SHA1

Just the way IDLE works. Unexpected, but certainly not a bug, I'd say.

Andreas

Grant Hagstrom wrote:
 A bug or feature in the IDLE of python 2.5?
 
 pre-step: save the following file to your computer:
 # file mylist.py
 jobs = [
 'Lions',
 'SysTest',
 'trainDD',
 'Cats',
 'train',
 'sharks',
 'whale',
 ]
 
 Step 1.
 
 copy, paste this script into the idle window. hit f5 to run.
 
 thefile = open( mylist.py)
 read_thefile = file.read(thefile)
 
 string_thefile = str(read_thefile)
 
 splitlist_thefile = string_thefile.split()
 
 it_starts = splitlist_thefile.index('jobs')
 
 Step 2.
 
 open a new window. close the window from step one. close the shell from
 the previous f5 execution.
 
 Step 3.
 
 paste and run this code.
 
 thefile = open(/home/banter/Desktop/mylist.py)
 read_thefile = file.read(thefile)
 
 #string_thefile = str(read_thefile)
 
 splitlist_thefile = string_thefile.split()
 
 it_starts = splitlist_thefile.index('jobs')
 
 Result: python remembers string_thefile even after both the shell and
 the original scripting window have been closed!
 bug or feature?
 
 Best,
 
 Grant
 
 
 
 
 ___
 Tutor maillist  -  Tutor@python.org
 http://mail.python.org/mailman/listinfo/tutor
-BEGIN PGP SIGNATURE-
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGX+PnHJdudm4KnO0RAsdhAKCBUU9HxXqHPZXo0BXqDEtQ+kBc1wCfS/kc
Tk1icM3HgifChP7wxKShM2k=
=fig0
-END PGP SIGNATURE-
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] bug or feature

2007-06-01 Thread Grant Hagstrom

A bug or feature in the IDLE of python 2.5?

pre-step: save the following file to your computer:
# file mylist.py
jobs = [
   'Lions',
   'SysTest',
   'trainDD',
   'Cats',
   'train',
   'sharks',
   'whale',
   ]

Step 1.

copy, paste this script into the idle window. hit f5 to run.

thefile = open(mylist.py)
read_thefile = file.read(thefile)

string_thefile = str(read_thefile)

splitlist_thefile = string_thefile.split()

it_starts = splitlist_thefile.index('jobs')

Step 2.

open a new window. close the window from step one. close the shell from the
previous f5 execution.

Step 3.

paste and run this code.

thefile = open(/home/banter/Desktop/mylist.py)
read_thefile = file.read(thefile)

#string_thefile = str(read_thefile)

splitlist_thefile = string_thefile.split()

it_starts = splitlist_thefile.index('jobs')

Result: python remembers string_thefile even after both the shell and the
original scripting window have been closed!
bug or feature?

Best,

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


[Tutor] Bug in python, or is it just 3am

2006-04-21 Thread ryan luna
Hey everyone, i believe i might have found a bug in
python? im not sure, heres a screen shot.
http://img151.imageshack.us/img151/4268/pythonbug8by.jpg
When i type
number + 1
and print it,
It adds one,
But when i use a number = number + 1
right after the value stays the same,
Now i thought that number = number + 1 just wasn't
vailed in python untill i tried it again and it
worked,
Is this a bug or im i just missing somthing,
Cause it is rather late.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Bug in python, or is it just 3am

2006-04-21 Thread ryan luna
HA! ignore me, im stupid, XD i knew i should have
waited untill morning =P,
No bug, the number = number was just point to the old
number which was one number lower,
sorry. night =P
Oh i see someone replied -_- sorry lol
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bug in python, or is it just 3am

2006-04-21 Thread Danny Yoo


On Fri, 21 Apr 2006, ryan luna wrote:

 HA! ignore me, im stupid, XD i knew i should have waited untill morning 
 =P, No bug, the number = number was just point to the old number which 
 was one number lower, sorry. night =P

Get some sleep.  *grin*
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Bug in python, or is it just 3am

2006-04-21 Thread Alan Gauld
 But when i use a number = number + 1
 right after the value stays the same,

I'm not sure what you mean by that.

 Now i thought that number = number + 1 just wasn't
 vailed in python untill i tried it again and it
 worked,

variable = variable + 1

is perfectly valid. It is not the normal math meaning of an equation 
however, it is an assignment statement. In a language like Smalltalk 
or Pascal they use a different symbol (:=) for assignment which 
is IMHO A Good Thing(TM)  And they traditionally read that 
symbol as becomes, thus:

variable := variable + 1

is read: variable becomes variable plus one

What it means is that variable takes on the previous value 
of variable plus one. So if it starts as 42 it ends as 43

This is such a common thing to do that Python actually 
has a shorthand for it:

variable += 1

And after all that, I've just realised that I don't discuss this 
at all in my tutorial so I need to add an explanation this weekend. 
So thanks for asking the question! :-)

HTH,

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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


Re: [Tutor] Bug in python, or is it just 3am

2006-04-21 Thread Liam Clarke
Hi Ryan,

I see what confused you; the


 number + 1
6
 print number
5


part. Yeah, it's only evaluating the the first one. So you're asking
it What's number + 1?
Whereas,

 number = number + 1

or

 number += 1

Is saying Make number equal number plus 1

Ha, it's all a learning experience.

Regards,

Liam Clarke

On 4/21/06, Alan Gauld [EMAIL PROTECTED] wrote:
  But when i use a number = number + 1
  right after the value stays the same,

 I'm not sure what you mean by that.

  Now i thought that number = number + 1 just wasn't
  vailed in python untill i tried it again and it
  worked,

 variable = variable + 1

 is perfectly valid. It is not the normal math meaning of an equation
 however, it is an assignment statement. In a language like Smalltalk
 or Pascal they use a different symbol (:=) for assignment which
 is IMHO A Good Thing(TM)  And they traditionally read that
 symbol as becomes, thus:

 variable := variable + 1

 is read: variable becomes variable plus one

 What it means is that variable takes on the previous value
 of variable plus one. So if it starts as 42 it ends as 43

 This is such a common thing to do that Python actually
 has a shorthand for it:

 variable += 1

 And after all that, I've just realised that I don't discuss this
 at all in my tutorial so I need to add an explanation this weekend.
 So thanks for asking the question! :-)

 HTH,

 Alan G
 Author of the learn to program web tutor
 http://www.freenetpages.co.uk/hp/alan.gauld


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

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


Re: [Tutor] Bug in python

2006-02-21 Thread Hugo González Monteverde
Hi, just a recommendation: try not to assume a bug beforehand, that's a 
quick way to get flames or just get plain ignored, especially in a list 
for beginners.

 The * is being given equal  priority to %.
 
 Why isn't % given higher priority than *?

Why should it? Doesn't it make sense that as '/'
  and * have the same precedence, '%' should have the same?

Check the precedence for python operators here:
http://www.byteofpython.info/read/operator-precedence.html
http://docs.python.org/ref/summary.html

Check the precedence for C operators here:
http://publications.gbdirect.co.uk/c_book/chapter2/expressions_and_arithmetic.html

So everything alright, also, left to right, as expected.


 Also,  why am I getting a syntax error  in the following?
 

To get more informative messages, save all those statements to a file, 
and then try and run the file. It is extremely easy to mess up 
indentantion and make mistakes in the interactive prompt. Aslo you'll be 
getting information about the line number and such.

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


[Tutor] Bug in python

2006-02-19 Thread Kermit Rose
 a = 1
 a2 = a%2
 a2
1
 8*a2
8
 8*(a%2)
8
 8*a%2
0
 

The * is being given equal  priority to %.

Why isn't % given higher priority than *?


Also,  why am I getting a syntax error  in the following?


The def in the definition of the second function is being highlighted.

IDLE 1.1.2  
 def transfac(v):
  a = v[0]
  b = v[1]
  c = v[2]
  d = v[3]
  m = v[4]
  n = v[5]
  z = v[6]
  k = v[7]
  k = k + 1
  a2 = a%2
  b2 = b%2
  c2 = c%2
  d2 = d%2
  ma = ((d - n * c)/(a*m + b))/2
  na = ((d - m * b)/(a * n +c) )/2
  j = 8 * a2 + 4 * b2 + 2 * c2 + d2
  if j == 0:  a,b,c,d,m,n = a/2,b/2,c/2,d/2,m,n
  if j == 1:  a,b,c,d,m,n = -1,-1,-1,-1,-1,-1
  if j == 2:  a,b,c,d,m,n = a,b/2,c,d/2,m,2*n
  if j == 3:  a,b,c,d,m,n = a,(a+b)/2,c,(d-c)/2,2*n+1
  if j == 4:  a,b,c,d,m,n = a,b,c/2,d/2,2*m,n
  if j == 5:  a,b,c,d,m,n = a,b,(a+c)/2,(d-b)/2,2*m+1,n
  if j == 6:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 7:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 8:  a,b,c,d,m,n = -1,-1,-1,-1,-1,-1
  if j == 9:  a,b,c,d,m,n = 2*a,a+b,a+c,(d-a-b-c)/2,2*m+1,2*n+1
  if j == 10:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 11: a,b,c,d,m,n = 2*a,a+b,c,(d-c)/2
  if j == 12:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  if j == 13: a,b,c,d,m,n = 2*a,b,a+c,(d-b)/2,m,2*n+1
  if j == 14: a,b,c,d,m,n = 2*a,b,c,d/2,2 *m,2*n 
  if j == 15:  a,b,c,d,m,n = a,a*na+b,a*ma+c,d-a*ma*na - b*ma - c * na,m+ma
n+na
  z = a * d + b * c
  v = [a,b,c,d,m,n,z,k]
  return  v 

 
comment:   the def in the following line has been highlighted.

def gcd(a,b):
  r2 = a
  r3 = b
  flag = 1
  while flag0:
if r3 == 0:
  return r2
  flag = 0
else:
  r1=r2
  r2=r3
  r3 = r1 - r2*(r1/r2)


def factor0(z):
  v = [1,0,0,341,1,1,341,1]
  v[3] = z
  v[6]= z
  flag = 1 
  while flag  0:
  v = transfac(v)
  g = gcd(z,v[0])
  if g1:
  if gz:
  return g
  flag = 0
  g = gcd(z,v[1])
  if g1:
  if gz:
  return g
  flag = 0  
  g = gcd(z,v[2])
  if g1:
  if gz:
  return g
  flag = 0  
  g = gcd(z,v[3])
  if g1:
  if gz:
  return g
  flag = 0
  if v[1]+v[2]+v[3]-v[4]==0:
  return v[0]+v[1]
  flag = 0
  if v[1]+v[2]-v[3]+v[4]==0:
  return v[0]+v[1]
  flag = 0
  if v[1]-v[2]+v[3]+v[4]==0:
  return v[0]+v[2]
  flag = 0
  if -v[1]+v[2]+v[3]+v[4]==0:
  return v[3]+v[1]
  flag = 0


SyntaxError: invalid syntax
  

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


[Tutor] Bug in python

2006-02-19 Thread John Fouhy
On 20/02/06, Kermit Rose [EMAIL PROTECTED] wrote:
  8*a%2
 0
 The * is being given equal  priority to %.

 Why isn't % given higher priority than *?

Calling it a bug is a bit harsh when it's documented that way :-)

See: http://docs.python.org/ref/summary.html

*, / and % all have the same precedence.  I guess the reasoning is
that / is (approximately) the inverse of * and % is remainder after
/.

 Also,  why am I getting a syntax error  in the following?

When you're using the interactive interpreter, you need to end a
function definition with an extra carriage return.
(at least, that's the way it works in the console version)

eg:

 def foo():
...  pass
... def bar():
  File stdin, line 3
def bar():
  ^
SyntaxError: invalid syntax

vs:

 def foo():
...  pass
...
 def bar():
...  pass
...


HTH!

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


Re: [Tutor] Bug in python

2006-02-19 Thread Alan Gauld
 8*a%2
 0


 The * is being given equal  priority to %.

 Why isn't % given higher priority than *?

Because that's the way Guido designed I guess. ;-)
Although why would you expect % to be higher precedence than *?
You can always use parentheses, and if in any doubt should
do so.

 Also,  why am I getting a syntax error  in the following?

 The def in the definition of the second function is being highlighted.

Its always good to send the complete error message.
Python errors are extremely informative whemn you can see them
in their entirety.

 def transfac(v):
  a = v[0]
  b = v[1]
  c = v[2]
  d = v[3]
  m = v[4]
  n = v[5]
  z = v[6]
  k = v[7]
  k = k + 1
  a2 = a%2
  b2 = b%2
  c2 = c%2
  d2 = d%2
  ma = ((d - n * c)/(a*m + b))/2
  na = ((d - m * b)/(a * n +c) )/2
  j = 8 * a2 + 4 * b2 + 2 * c2 + d2
  if j == 0:  a,b,c,d,m,n = a/2,b/2,c/2,d/2,m,n
  if j == 1:  a,b,c,d,m,n = -1,-1,-1,-1,-1,-1
  if j == 2:  a,b,c,d,m,n = a,b/2,c,d/2,m,2*n
  if j == 3:  a,b,c,d,m,n = a,(a+b)/2,c,(d-c)/2,2*n+1

It might be better to use an elif chain here. It makes little differemce in 
this
case but is more conventional for long chains like this.

if x == 1: f(y)
elif x == 2: g(y)
elif x == 3: ...etc
else: catch errors here

 comment:   the def in the following line has been highlighted.

Comments in Python startt with a # character and extend
to the end of line. I appreciate its not part of the code but
I thought I'd point it out :-)

 def gcd(a,b):
  r2 = a
  r3 = b
  flag = 1
  while flag0:
if r3 == 0:
  return r2

When you doi a return execution leaves the function so
any lines after return will be ignored. This is like C.
Buit its not your error, Python simply ignores them.


However the error may be above the indicated line but
since your post has lost all indentation its hard to tell.


 SyntaxError: invalid syntax

We really need to see the full error plus the actual code
with indentation etc  for say 10 lines above and below the
marked line.

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld


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