I am brand new to programming. I am using the Dive into Python book and am trying to run the first example, humansize.py. I have copied and pasted the code into Idle, the Python shell and keep coming up with the same syntax error: "multiple statements found while compiling a single statement."
I am downloading the code into BBEdit and then copying and pasting it into Idle. I have looked online and people said it could be a tab versus space issue. But I've went through the code and it looks identical to the book, I've even deleted and reinserted 4 spaces in all the lines of code and I'm still getting the error.
It's frustrating because I am sure that it's a simple issue but I've done everything that I know of, (in terms of trying to research the problem) to get it to work. If it is a space versus tabs issue, do any of you know where I can go and learn how to do the process of copying and entering code into Idle properly? I am a TRUE beginner.
I'd appreciate any assistance from the community. Thank you!
I am running a Mac OSX - V.10.7.5. I am using the latest version of the Dive into Python book and Python 3.3.
The code is below:
>>> '''Convert file sizes to human-readable form.
Available functions:
approximate_size(size, a_kilobyte_is_1024_bytes)
takes a file size and returns a human-readable string
Examples:
>>> approximate_size(1024)
'1.0 KiB'
>>> approximate_size(1000, False)
'1.0 KB'
'''
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 size to human-readable 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 < multiple:
return '{0:.1f} {1}'.format(size, suffix)
raise ValueError('number too large')
if __name__ == '__main__':
print(approximate_size(1000000000000, False))
print(approximate_size(1000000000000))
**SyntaxError: multiple statements found while compiling a single statement**
>>>