Skip to content

Instantly share code, notes, and snippets.

@Kaylen-Travis-Pillay
Created June 24, 2017 07:09
Show Gist options
  • Save Kaylen-Travis-Pillay/767c1e642b0905abb61a330914dd4d1e to your computer and use it in GitHub Desktop.
Save Kaylen-Travis-Pillay/767c1e642b0905abb61a330914dd4d1e to your computer and use it in GitHub Desktop.
Assignment 1 Solution
####################################################################
# Assignment 1 Solution #
# #
# Author: Kaylen Travis Pillay #
# Date: 2017/06/24 #
# #
# NB: #
# Please note that this is a SUGGESTED solution. Therefore #
# if your our output is the same, you should take the coding #
# style from this suggestion. #
####################################################################
# The assignment document can be found at :
# https://docs.google.com/document/d/14bHGOrUUgVfzH6Lvbs9FHpJKJ5Q5fp5vsGezjZpwyPo/edit?usp=sharing
import math
def CalculateEnergy( richterScaleData ):
#This function calculates the energy produced by an earthquake.
#The calculation follows the rules outlined in the wiki below:
#https://en.wikipedia.org/wiki/Richter_magnitude_scale
energy = math.pow(10, ( 1.5 * richterScaleData ) + 4.8 )
return energy
def CalculateTNT( energy ):
#This function calculates the tons of TNT equivalent of energy given
#The calculation used can be found here:
#https://en.wikipedia.org/wiki/TNT_equivalent
TNT = energy / (4.184 * (10e8))
return TNT
def TestMeasurements():
#This function displays the test data to the user
#The test data is taken from the assignment question and is a requirement
print( "Sample Data: \n" )
print( "Richter Scale: ", 1.0)
print( " Energy: ", CalculateEnergy( 1.0 ))
print( " Tons of TNT: ", CalculateTNT(CalculateEnergy( 1.0 )))
print()
print( "Richter Scale: ", 5.0)
print( " Energy: ", CalculateEnergy( 5.0 ))
print( " Tons of TNT: ", CalculateTNT(CalculateEnergy( 5.0 )))
print()
print( "Richter Scale: ", 9.1)
print( " Energy: ", CalculateEnergy( 9.1 ))
print( " Tons of TNT: ", CalculateTNT(CalculateEnergy( 9.1 )))
print()
print( "Richter Scale: ", 9.2)
print( " Energy: ", CalculateEnergy( 9.2 ))
print( " Tons of TNT: ", CalculateTNT(CalculateEnergy( 9.2 )))
print()
print( "Richter Scale: ", 9.5)
print( " Energy: ", CalculateEnergy( 9.5 ))
print( " Tons of TNT: ", CalculateTNT(CalculateEnergy( 9.5 )))
print( "===================================================")
def main():
# The first step would be to aquire the users details
TestMeasurements()
#Getting the users richter scale value
try:
#NOTE HERE:
#If the user enters a alphabet instead of a numerical value then an exception is raised
users_richter = float(input("Enter your Richter Scale value: "))
if users_richter >= 0.0 and users_richter <= 10.0:
#The users richter scale is valid
print( "Richter Scale: ", users_richter)
print( " Energy: ", CalculateEnergy( users_richter ))
print( " Tons of TNT: ", CalculateTNT(CalculateEnergy( users_richter )))
else:
#The users richter-scale value is not it the correct range!
raise Exception
except:
#This is the information shown when an exception is thrown
print(" Sorry but that is an invalid Richter Scale value! ")
print(" Richter-Scale values are from 0.0 -> 10.0 ")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment