Python Forum
I'm teaching myself python and i'm stuck...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I'm teaching myself python and i'm stuck...
#1
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!
Reply
#2
please read the following: BBCODE
re-post using code tags. Also, when pasting use shoft-ctrl-v to preserve indentation.
Reply
#3
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Beginner stuck in Python book for kids mic81k 11 1,194 Nov-27-2023, 04:28 AM
Last Post: deanhystad
  Stuck on python quiz challenge baesian 2 2,176 Aug-16-2020, 12:52 AM
Last Post: scidam
  Learning python, stuck on some code. stanceworksv8 2 3,474 Apr-02-2019, 01:51 AM
Last Post: stanceworksv8
  I finsh the basics of python but I'm stuck on how to link code to GUI Qubayel 5 4,335 Apr-04-2017, 07:18 AM
Last Post: Ofnuts

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020