Jump to content

So here 's my submission for the week 14 python stuff


G+_Dan Sarauer (N3m1sys)
 Share

Recommended Posts

I was running your code and first things first it works good example.

 

The only feedback I would like to add is in the listpests function where you use a try block to determine if a file exists. According to python PEP8 it should be the minimum about of code followed by the specific exception you want to catch.  The code between the try block and the except should be exactly what you like to test. In your case it was

 

cKennel = open('CodingKennels.txt', 'r')

 

followed by the

except "some exception name":

 

 then what you would like to happen to prevent this except followed by an else statement if you except does not contain a return statement

 

This make debug easier as it allows you to find the specific exception. If you use a 'except: ' known as a bare exception  it will catch all exceptions for example TypeErrors, AssertionErrors, SystemErrors, ValueErrors and much much more. This make it really hard to find the specific exception as it disguises other problems. This is a nightmare for debugging.

 

So in you code since your are testing to see whether a file exists then it should be

 

try:

  cKennel = open('CodingKennels.txt', 'r')

except IOError:

   print some message

else:

   

 

 

IOError deals with all things relating to input and output exceptions. If an exception is  thrown the print message is executed and if not the else statement is run.

 

This allows you to know exactly where the exception is thrown and what caused it making debugging a  hell of lot easier.

 

For example assume an IOError was thrown and you did not know what it was you could google 'what does IOError mean' and get the result.

 

try:

   

except:

    do something

 

The code above makes it harder to detemine what exception has occurred. Because each time an exception occurs it is disguised by do some thing.

 

 This is based on PEP8 guide for good python etiquette.

 

Also python library guide has a list on all exceptions:

 

https://docs.python.org/2/librar/exceptions.html

 

Only use bare except if you want to catch all error or if you do not really care what cause the exception.

 

Feel feel to comment on anything you do not like in my code

Link to comment
Share on other sites

cool example but I noticed on line 34 of your code you are used the name type as a variable name. Well the name type is actually a reserved python word and basically it is actually considered bad practice to use a reserved python word.  By do so the python word is now a reference to your variable

 

by the way:

type is a python word used to determine the data type of an object

 

>>> type(2)

 

>>> type(2.0)

 

>>> type([])

Link to comment
Share on other sites

I was testing out your code and I noticed a few bugs in your code. In the givePetUp function there is a bug because of the way you have implemented the code.

 

 

if you run option 2 which is the option for giving up pets. There is a bug there.

for example

 

Name (Alpha characters only please): adam

Type of animal (example: cat):cat

Sex (m/f):m

Age: g # inputted an incorrect value

Breed (example: Golden Retriever):golden retreiver

Notes about them (example: Playful):playful

 

now the program ask me to input age because I entered it wrong

 

Age must be a number...

Age:

 

now if enter it wrong again the code let me pass

 

Age must be a number...

Age:apples

 

You enterred:

Name: adam

Type: cat

Sex: m

Age: apples

Breed: golden retreiver

Note: playful

 

Is the enterred information correct?(y/n):y

 

when I hit y the code accepts my input and returns me to menu accepting my code

 

How can we help you today?:

1. Display a list of pets available.

2. Put pet up for adoption.

3. Adopt a pet.

4. Exit

 

Please enter you selection:

 

 

second thing you have repeated yourself in the function two times you ask to user to enter their age in line 35 and then in line 58 where you check if the user has entered the correct info. The problem lies in line 60 the validInput is set to True regardless whether the user enters the correct info.  Line 60 is always executed each time it breaks out of the try block.

 

The best way to ensure that user enters the correct information is to put the validInput inside and try, except else block within a while conditon. The else block will never be executed if the condition are not met

 

while :

     try:

         int(age) #Age must be an integer

    except ValueError:

       print '\nAge must be a number...'

       age = raw_input('Age:')

  else:

       validInput = True

 

 

 

 You then repeat your code in  line 35 and 63. The

 

Instead of allowing to user to enter all the information about the pets before checking. Move all the information you want to check inside a while try, else block that way you do not repeat yourself

 

 

thirdly

 

while invalidInput == False:

    < some code>

 

this can be rewritten as :

 

while not invalidInput:

   

 

 

In line 41 you can actually move the variable to the line 29 or something. Each time your run your while loop your actually creating a new variable called validInput = False. But here the thing each always be false unless it becomes true in the try block. So there no need to recreate it

 

 

Apart from that the idea of kennel is great.

Link to comment
Share on other sites

by the way there was a typo in my comment earlier it should have been like this.

 

 

while True:

     try:

           print '\nAge must be a number...'

           age = raw_input('Age:')

     except ValueError:

          

     else:

              validInput = True

               break

 

you should use an infinity loop if you not sure how may times your user will input bad data. And then use a break to break out of the while loop when the data is correct.

 

 

not this way:

 

while :

     try:

         int(age) #Age must be an integer

    except ValueError:

   

  else:

       validInput = True

Link to comment
Share on other sites

 Share

×
×
  • Create New...