Dec-05-2017, 10:16 PM
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!
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!