[Tutor] Removing Unnecessary Indentation and other problems

ALAN GAULD alan.gauld at btinternet.com
Sat Nov 16 12:22:55 CET 2013


Including tutor list. 
Please use ReplyAll when responding to the list.
 

Sorry about that. I am new to this so I didnt know I have to be very specific towards every detailIt depends on what you are asking, but in this case different downloads include different packages 
so we need to know which OS, which version and which web site you got it from. In this case I'll 
assume you got it from the python.org site?

. I downloaded 'Python 3.3.2 Windows x86 MSI Installer (Windows binary -- does not include source)' Thats fine it does have Tkinter included. Personally, for Windows users I recommend 
getting the Activestate.com version of Python instead. It includes some nice extra features 
and it integrates Python more closely with the Windows environment. But thats not the 
cause of your problem here.

from Tkinter import *
>
>
>This is the problem. In Python v3 they changed the name of the Tkinter module from 
Tkinter to tkinter(no caps). I think it was just to make the name more consistent with 
other modules... The result is you need to change your import to be

from tkinter import *


For the second part of the question.. I don't know which font I am using but I think its default font? OK, So in that case how are you running the program? Are you using IDLE or a CMD shell window? 
Or something else? One of the problems of using tabs, and of printing directly in general, is that the 
program may be displaying in many different environments and therefore with different fonts. 
You therefore have limited control over the output. That's why generating an HTML page which 
can have font information embedded into it and use tables etc to control layout is a more reliable 
option if you need precise control of layout.

If precise control is not so necessary and you think the font will be monospaced then you can 
do some things in a print statement that will help, just be aware that it may not be 100% effective.

while sMainContinue == "y"or sMainContinue =="yes" or sMainContinue =="YES"or sMainContinue =="Y":
>
>    userMainInput = (input("Please select one option from menu: "))
>
>You are making this hard for yourself./ Its usually better to convert the input to lowercase 
(or uppercase if you prefer!) and then compare. So your line above could be:

while sMainContinue.lower() in ('y','yes'):

Also in the input() line you don't need the outer set of parens. But see below...

#Robust Options: if input is a letter
>
>    if userMainInput.isdigit():
>        userInput = int(userMainInput)Again its more common in Python to just do the conversion at the input stage and catch an 
exception if the data is bad., so that would look like

   try:
      userInput = int( input("Please select one option from menu: ") )

   except ValueError:
       # deal with non numeric input here     

#Options1
>        if userInput == 1:
>            print("\n********Road Information**********")
>            print("Road Name","Speed",sep = "\t\t")
>            for key,value in Dic.items():
>                print(key,value,sep = "\t\t")And here is the display part and it relies on tabs. Tabs work on fixed column positions so to be 
consistent you would need to check the length of the strings and insert the correct number of tabs 
depending on whether the string was shorter or longer. In your case it could be anywhere from 
1 tab to 3 tabs... That's messy and using tabs for formatting like that is not recommended.

Instead Python offers a formatting mechanism that you can apply to a string before it is 
printed which gives you much more control over spacing and justification etc.

You could read about it in the official docs but there is a more accessible tutorial here:

http://infohost.nmt.edu/tcc/help/pubs/python/web/new-str-format.html

The key is to use fixed length string fields to output your data.
You may optionally choose to left justify the name and right justify the speeds, 
or you could just left justify everything. The choice is yours.

But remember format() still relies on the user having monospaced fonts for it to work properly.

HTH,

-- 
Alan G 
Author of the Learn to Program web site 
http://www.alan-g.me.uk/ 
http://www.flickr.com/photos/alangauldphotos 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20131116/5342fe46/attachment-0001.html>


More information about the Tutor mailing list