[Tutor] Character creator program

Alan Gauld alan.gauld at btinternet.com
Wed May 12 19:12:21 CEST 2010


"Ben Millane" <ben at richmondtravelsolihull.co.uk> wrote

> My problem is, that although I have the program working how I want it 
> to work in general. I still have an issue with the user input section. 
> If my user inputs any of the options from the menu, the program works 
> as it should. But say if the user accidentally pressed enter with no 
> option entered the program ends with an error message, 
> or if they put in the incorrect number or a letter, it returns an error.

Thats what you told it to do!

Work through the logic of all those if/elifs and see where a blank 
entry will go. Add the logic to catch it there.

It will help if you use functions to reduce the amount of code between 
the if/elifs. That will make the code much more readable.

Another option is then to use a dictionary to call the appropriate 
function, and if the key is not in the dictionary respond appropriately.

Also you xccould move the "make a new selection bit out of 
the individual sections to the end of the while loop. It saves 
repetition (The DRY principle - Don't Repeat Yourself) and
again keeps the code readable.
You will see a short example of what you are doing in my tutorial 
under "Branching". Look at how I handled the invalid 
input situation in the "putting it all together" section for some ideas.


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/




More information about the Tutor mailing list