Re: [Tutor] Help with exceptions please

2011-05-14 Thread Timo

On 14-05-11 05:05, Wayne Werner wrote:
On Fri, May 13, 2011 at 7:48 PM, Lea Parker lea-par...@bigpond.com 
mailto:lea-par...@bigpond.com wrote:


Hello

I have another assignment I am trying to fine tune. The idea is to
make sure exceptions work. I have come up with a problem when
testing my code. When I enter a golf score for a player the
program returns the ‘error that has occurred’. I only want it to
do this if someone doesn’t enter an number. snip


I don't have a lot of time to look at it right now, but what happens 
when you remove the final except?


Having a catch-all can hide all sorts of exceptions. If you remove 
that, then python should tell you what kind of error it got. That 
traceback will be much more helpful than 'error that has occurred'.
I agree. Something is throwing an exception you don't expect. If you do 
want to catch it and exit gracefully, you can replace:

except:
print 'An error occured.'
with:
except Exception, exc:
print 'An error occured:\n' + exc

Cheers,
Timo




If that doesn't help you fix the problem, then remember the three 
things you need:


1. What did I do?
2. What did I expect would happen?
3. What happened instead?

For 3, that also means that you should post all the (relevant) output. 
That means errors, and usually correct output, especially if there's 
not a whole lot of it.


HTH,
Wayne


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


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


[Tutor] Help with exceptions please

2011-05-13 Thread Lea Parker
Hello

 

I have another assignment I am trying to fine tune. The idea is to make sure
exceptions work. I have come up with a problem when testing my code. When I
enter a golf score for a player the program returns the 'error that has
occurred'. I only want it to do this if someone doesn't enter an number.

 

Could I have some comments on my code for the first program of my assignment
if you have some time please. 

 

Assignment Question:  The Springfork Amateur Golf club has a tournament
every weekend.

The club president has asked you to write two programs:

1. A program that will read each player's name and golf score as keyboard

input and then to save these records in a file named golf.txt. (Each record

will have a field name for the player's name and a field for the player's

score.

2. A program that reads the records from the golf.txt and displays them.

 

 

 

def main():

 

try:

# Get the number of players

golf_scores = int(raw_input('How many players playing this weekend?
'))

 

# Open a file to hold player names and scores

player_data = open('golf.txt', 'w')

 

# Get the players name and score and write it to the file

for count in range(1, golf_scores + 1):

# Get the name name and score for player

print 'Enter the name and score for player #' + str(count)

name = raw_input('Name: ')

score = int(raw_input('Score: '))



# Write the data as a record to the file golf.txt

player_data.write(name + '\n')

player_data.write(score + '\n')

 

# Display a blank line

print

 

# Close the file

player_data.close()

print 'Players and their score have been written to golf.txt.'

 

except IOError:

print 'An error occured trying to write information to file.'

 

except ValueError:

print 'A numberic value must be entered.'

 

except:

print 'An error occured.'

 

 

 

# Call the main function

main()

 

Thanks in advance.

Lea

 

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


Re: [Tutor] Help with exceptions please

2011-05-13 Thread Wayne Werner
On Fri, May 13, 2011 at 7:48 PM, Lea Parker lea-par...@bigpond.com wrote:

 Hello



 I have another assignment I am trying to fine tune. The idea is to make
 sure exceptions work. I have come up with a problem when testing my code.
 When I enter a golf score for a player the program returns the ‘error that
 has occurred’. I only want it to do this if someone doesn’t enter an number.
 snip


I don't have a lot of time to look at it right now, but what happens when
you remove the final except?

Having a catch-all can hide all sorts of exceptions. If you remove that,
then python should tell you what kind of error it got. That traceback will
be much more helpful than 'error that has occurred'.

If that doesn't help you fix the problem, then remember the three things you
need:

1. What did I do?
2. What did I expect would happen?
3. What happened instead?

For 3, that also means that you should post all the (relevant) output. That
means errors, and usually correct output, especially if there's not a whole
lot of it.

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