# -------------------------------------------------------------------
# Import libraries
# -------------------------------------------------------------------
import math
# -------------------------------------------------------------------
# Constants
# -------------------------------------------------------------------
# -------------------------------------------------------------------
# Global variables
# -------------------------------------------------------------------
fileSize = ""
totalBits = 0
networkSpeed = ""
bitsPerSecond = 0.0
downloadTime = 0.0
# -------------------------------------------------------------------
# Subprograms
# -------------------------------------------------------------------
# convert a string like "1.7 MiB" to the number of bits (e.g 14260634)
def convertSizeToBits(pFileSize):
parts = pFileSize.split(" ")
quantity = float(parts[0])
unit = parts[1]
if unit == "B":
quantity *= 8
elif unit == "KiB":
quantity *= 8 * 1024
elif unit == "MiB":
quantity *= 8 * (1024 ** 2)
elif unit == "GiB":
quantity *= 8 * (1024 ** 3)
elif unit == "TiB":
quantity *= 8 * (1024 ** 4)
else:
print("Unknown unit, assuming you entered the number of bits")
return math.ceil(quantity)
# convert a string like "10 Mbps" to the number of bits per second (e.g. 10000000)
def convertSpeedToBitsPerSecond(pSpeed):
parts = pSpeed.split(" ")
speed = float(parts[0])
unit = parts[1]
if unit == "Kbps":
speed *= 1000
elif unit == "Mbps":
speed *= (1000 ** 2)
elif unit == "Gbps":
speed *= (1000 ** 3)
elif unit == "Tbps":
speed *= (1000 ** 4)
else:
print("Unknown unit, assuming you entered the number of bits per second")
return speed
# convert a time in seconds like 0.3 to a human readable string (e.g "300 ms")
def convertTime(pTimeInSeconds):
unit = "s"
duration = pTimeInSeconds
if duration < 1:
unit = "ms"
duration *= 1000
elif duration > 60:
unit = "mins"
duration /= 60
return "{:.1f} {}".format(duration, unit)
# -------------------------------------------------------------------
# Main program
# -------------------------------------------------------------------
print("Download speed calculator")
# find the size of the file in bits
fileSize = input("Enter file size (e.g. 1.7 MiB)")
totalBits = convertSizeToBits(fileSize)
print("Total number of bits: ", totalBits)
# find the network speed in bits per second
networkSpeed = input("Enter network speed (e.g. 55 Mbps)")
bitsPerSecond = convertSpeedToBitsPerSecond(networkSpeed)
print("Total bits per second: ", bitsPerSecond)
downloadTime = totalBits / bitsPerSecond
print("Download time:", convertTime(downloadTime))
# Challenges
# 1) Replace every 1024 with a constant called BYTES_IN_KIBIBYTE
# 2) Replace every 1000 with a constant called BYTES_IN_KILOBYTE
# 3) Adjust convertTime to show very long times in hours rather than minutes
You are not logged in
`¬
1!
2"
3£
4$
5%
6^
7&
8*
9(
0)
-_
=+
TAB
Q
W
E
R
T
Y
U
I
O
P
[{
]}
ENTER
CAPS
A
S
D
F
G
H
J
K
L
;:
'@
#~
ENTER
SHIFT
\|
Z
X
C
V
B
N
M
,<
.>
/?
SHIFT
SPACE
Welcome to type with code
Start typing or press to get going!
The create code you'll be racing to type out is all about:
Made with create.withcode.uk
This code was saved on Friday 4th Nov 2022 press here to edit, run or debug it