Python Forum

Full Version: I'm teaching myself python and i'm stuck...
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm using a book to teach myself python and the book is telling me to run the code on the command line but im not sure i did it right because none of the final lines i typed up lit up like the other ones did.

here is what i did:
>>> SUFFIXES = {1000: [ 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ],
1024: [ 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB' ] }
>>> def approximate_size(size, a_kilobyte_is_1024_bytes=True) :
''' Convert a file to human-pradable form
Keyword arguments:
size -- file size in bytes
a_kilobyte_is_1024_bytes -- if True (default), use multiples of 1024
if False, use multiples of 1000
Returns: string
'''
if size < 0:
raise ValueError( 'number must be non-negative' )
multiple = 1024 if a_kilobyte_is_1024_bytes else 1000
for suffix in SUFFIXES[multiple]:
size /= multiple
if size < multipl:
return '{0:.1f} {1}' .format(size, suffix)
raise ValueError( 'number too large' )
if _name_ == '_main_':
print(approximate_size(1000000000000, False) )
print(approximate_size(1000000000000) )
c:\home\diveintopython3\examples>
c:\python31\python.exe
humansize.py
1.0 TB
931.3 GiB

The bottom five lines of the above code is where i'm stuck, i've tried typing it multiple different ways and i can't figure out where i'm going wrong. If anyone could help me or explain what im doing wrong that would be great, thank you!
please read the following: BBCODE
re-post using code tags. Also, when pasting use shoft-ctrl-v to preserve indentation.
Please refrain from private mail as the forum is for all to see.
click on the BBCODE link above to find out how to use code tags.
Basically, paste your code using shift-ctrl-v, highlight and click on the python icon in the editor toolbar.