G+_Dan Sarauer (N3m1sys) Posted April 29, 2014 Share Posted April 29, 2014 So here's my submission for the week 14 python stuff. I took the ideas you (PadreSJ & Shannon Morse) were talking about with Dale Chase and built a little kennel with it. Hope you all enjoy! https://github.com/dsarauer/python_examples/tree/a0cb83ac79353289076f0ac72fecaa90839f42d1 Link to comment Share on other sites More sharing options...
G+_Dan Sarauer (N3m1sys) Posted April 29, 2014 Author Share Posted April 29, 2014 I should mention, I welcome feedback from anyone interested in giving it! Link to comment Share on other sites More sharing options...
G+_Jayunderwood Kent Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
G+_Jayunderwood Kent Posted April 30, 2014 Share Posted April 30, 2014 this also applys to the adopt pet function Link to comment Share on other sites More sharing options...
G+_Dan Sarauer (N3m1sys) Posted April 30, 2014 Author Share Posted April 30, 2014 Great feedback, I'll have to take a look at that. It's been a long time since I've had to use the try/catch so I'll use my code here as a good refresher of that. I hadn't heard of the PEP8 guide, so thanks for the heads up and I'll take a look! Link to comment Share on other sites More sharing options...
G+_Jayunderwood Kent Posted April 30, 2014 Share Posted April 30, 2014 Daniel Sarauer no problem apart from the example you used was very good. here a link to PEP8 guide http://legacy.python.org/dev/peps/pep-0008/ Link to comment Share on other sites More sharing options...
G+_egbie anderson Posted April 30, 2014 Share Posted April 30, 2014 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 More sharing options...
G+_egbie anderson Posted May 1, 2014 Share Posted May 1, 2014 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 More sharing options...
G+_Dan Sarauer (N3m1sys) Posted May 1, 2014 Author Share Posted May 1, 2014 Wow great catch egbie anderson...I'll definitely take a look at that and make some corrections. Link to comment Share on other sites More sharing options...
G+_egbie anderson Posted May 1, 2014 Share Posted May 1, 2014 Daniel Sarauer no problem Link to comment Share on other sites More sharing options...
G+_egbie anderson Posted May 1, 2014 Share Posted May 1, 2014 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 More sharing options...
G+_Dan Sarauer (N3m1sys) Posted May 1, 2014 Author Share Posted May 1, 2014 Thanks for the update on that...I've updated my code now to include some of the suggestions I've received. I didn't include the infinity loop you suggested just now, but everything seems to work now (hopefully less the undocumented features) Link to comment Share on other sites More sharing options...
G+_egbie anderson Posted May 1, 2014 Share Posted May 1, 2014 cool I shall enjoy testing your code Link to comment Share on other sites More sharing options...
Recommended Posts