[Tutor] ASSIST A BEGINNER PLEASSE

alexkleider alexkleider at protonmail.com
Wed Nov 11 21:45:20 EST 2020

```‐‐‐‐‐‐‐ Original Message ‐‐‐‐‐‐‐
On Wednesday, November 11, 2020 2:46 PM, Leonard Ujomu via Tutor <tutor at python.org> wrote:

> If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you fill in the gaps in the calculate_storage function below, which calculates the total number of bytes needed to store a file of a given size
> def calculate_storage(filesize):    block_size = 4096    # Use floor division to calculate how many blocks are fully occupied    full_blocks = 8192    # Use the modulo operator to check whether there's any remainder    partial_block_remainder = 4097 % 2    # Depending on whether there's a remainder or not, return    # the total number of bytes required to allocate enough blocks    # to store your data.    if partial_block_remainder > 0:        return block_size    return full_blocks
> print(calculate_storage(1))    # Should be 4096print(calculate_storage(4096)) # Should be 4096print(calculate_storage(4097)) # Should be 8192print(calculate_storage(6000)) # Should be 8192
>
> After running, i get the following result
> 4096409640964096
>
> Where is my error please??? Thank you
>
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor

What follows is a direct copy of a file I used to try first
to figure out the logic of your code and then, after failing
that, suggest a simpler solution.
Hope it helps.
Remember: use plain text!!!

#!/usr/bin/env python3

# File: 2try.py

'''
If a filesystem has a block size of 4096 bytes, this means that a file comprised of only one byte will still use 4096 bytes of storage. A file made up of 4097 bytes will use 4096*2=8192 bytes of storage. Knowing this, can you fill in the gaps in the calculate_storage function below, which calculates the total number of bytes needed to store a file of a given size

def calculate_storage(filesize):
"""
I've tried to adjust the indentation but can't follow the logic of
"""
block_size = 4096    # Use floor division to calculate how many blocks are fully occupied
full_blocks = 8192    # Use the modulo operator to check whether there's any remainder
partial_block_remainder = 4097 % 2    # Depending on whether there's a remainder or not, return
# the total number of bytes required to allocate enough blocks    # to store your data.
if partial_block_remainder > 0:
return block_size
return full_blocks
print(calculate_storage(1))    # Should be 4096
print(calculate_storage(4096)) # Should be 4096
print(calculate_storage(4097))
'''

def calculate_storage(filesize, block_size=4096):
"""
The following code is much simpler and seems to provide what you
want. Notice <block_size> has been made a named parameter which
will give you flexibility in case you ever need to apply this
function to a file system that has a differing block size.
As an editorial aside, it would seem to me that a calculate_blocks_required
would be a more useful function. (Simply delete the " * block_size" at the end.
"""
blocks_required, extra = divmod(filesize, block_size)
if extra:
blocks_required += 1
return blocks_required * block_size

print(calculate_storage(1))  # Should be 4096
print(calculate_storage(4096))  # Should be 4096
print(calculate_storage(4097))  # Should be 8192
print(calculate_storage(6000))  # Should be 8192

What_you_got = """
After running, i get the following result
4096
4096
4096
4096

Where is my error please??? Thank you
"""

what_I_got = """
alex at X1v2:~\$ python3 2try.py
4096
4096
8192
1,1           Top

```