Python Forum

Full Version: Create a bits & bytes conversion table
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hiii! I am doing homework about converting bits, bytes, KB and so on. It works fine for me to convert the number but I am struggling with the bonus. This is the instruction:

BEFORE or AFTER you print out the conversion table, print out the largest amount of memory first, convert to an integer and then print out the next smallest using the remaining fractional amount and so on until you use up all the bits.
For example, if given 1 553 032 Bytes the bonus output should be:
1 MB, 492 KB, 648 Bytes

Can anyone help me to solve the issue? And is there a more efficient way to calculate and convert.

import sys
def main():

    print("====================================")
    print("              GGGGGGGGG             ")
    print("       Bits & Bytes Revisited       ")
    print("====================================")

    print("What size of memory would you like? \n Select B (for bytes), \n KB (for kilobytes), \n MB (for megabytes) \n GB (Gigabytes) \n TB (for terabytes) \n")

    Size=input("B, KB, MB, GB or TB? ")

    Memory=float(input("\nHow much memory do you need? "))
    print("\nYour input was", "%d"%(int(Memory))+Size)

    if Size == "B":
        B = (Memory)
        Bits = (Memory)*1024
        KB = (Memory)/1024
        MB = (KB)/1024
        GB = (MB)/1024
        TB = (GB)/1024

    if Size == "KB":
        B = (Memory)*1024
        Bits = (B)*8
        KB == (Memory)
        MB = (Memory)/1024
        GB = (MB)/1024
        TB = (GB)/1024
        List = [Bits,B,KB,MB,GB,TB]
        
    if Size == "MB":
        KB = (Memory)*1024
        B = (KB)*1024
        Bits = (B)*8
        MB = (Memory)
        GB = (MB)/1024
        TB = (GB)/1024
        List = [Bits,B,KB,MB,GB,TB]
        
    if Size == "GB":
        MB = (Memory)*1024
        KB = (MB)*1024
        B = (KB)*8
        Bits = (B)*1024
        GB = (Memory)
        TB = (GB)/1024
        List = [Bits,B,KB,MB,GB,TB]
        
    if Size == "TB":
        GB = (Memory)*1024
        MB = (GB)*1024
        KB = (MB)*8
        B = (KB)*1024
        Bits = (B)*1024
        TB = (Memory)

    print("\n======== Conversion Table =========")
    print("Bits: ",Bits)
    print("Bytes: ", B)
    print("Kilobytes: ",KB)
    print("Megabytes: ",MB)
    print("Gigabutes: ",GB)
    print("Terabytes: ",TB)

    calc(int(Memory))

    restart=input("\nDo you wish to start again? ")
    if restart.lower()== "yes":
        main()

    else:
        sys.exit()

def calc(Memory):
    units = ["Bytes", "KB", "MB", "GB", "TB"]
    output = []
    num = calc
    for unit in units:
        if num <= 0:
            break
        number = str(num & 1023) + unit
        output.append(number)
        num = num >> 10
    output.reverse()
    print (output)

main()
This is the error:
Error:
Traceback (most recent call last): File "C:\Users\user\Documents\Computer Sciece\C_challenge.py", line 96, in <module> main() File "C:\Users\user\Documents\Computer Sciece\Cchallenge.py", line 74, in main calc(int(Memory)) File "C:\Users\user\Documents\Computer Sciece\C_challenge.py", line 88, in calc if num <= 0: TypeError: '<=' not supported between instances of 'function' and 'int'
what is supposed to do line #79 num = calc? in it you simply assign the function object calc to name num
def calc(Memory):
units = ["Bytes", "KB", "MB", "GB", "TB"]
output = []
num = Memory
for unit in units:
if num <= 0 :
break
tmp = str(num & 1023) + " " + unit
output.append(tmp)
num = num >> 10
output.reverse()
print (",").join(output)

I have changed a bit but the result says:
Traceback (most recent call last):
File "C:\Users\user\Documents\Computer Sciece\ChiewHui_challenge.py", line 96, in <module>
main()
File "C:\Users\user\Documents\Computer Sciece\ChiewHui_challenge.py", line 74, in main
calc(int(Memory))
File "C:\Users\user\Documents\Computer Sciece\ChiewHui_challenge.py", line 94, in calc
print (",").join(output)
AttributeError: 'NoneType' object has no attribute 'join'