Python Forum
Create a bits & bytes conversion table
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create a bits & bytes conversion table
#1
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'
Reply


Messages In This Thread
Create a bits & bytes conversion table - by CH13 - Mar-10-2020, 06:17 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help in reducing the number of bits for gps latitude and longitude sohaikia 1 1,901 Jan-12-2021, 08:17 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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