To answer your question in the most literal sense: energy()
should be self.energy(en)
. But if you understood classes, you'd know that. Your code is problematic in other areas as well.
You really need to think about what you're trying to achieve:
Read richter scale. Reread if wrong.
Calculate joules from scale.
Calculate TNT quantity from joules.
Print information.
Repeat.
Your code should reflect this. If you look at stricter_richter.read_loop
, that's exactly what happens.
The choice of words in the code suggests confusion about the difference between a function and a data point. That's why energy
has been renamed to calc_energy
and TNT
to calc_TNT
because they produce an output related to its input; they are not outputs in and of themselves. Related variables (like richter
, energy
and TNT
) should exist in the same scope and so they are calculated in the same loop.
The below builds upon Lafexlos' answer.
class stricter_richter(object):
tnt_joule_ratio = 4.184 * (10 ** 9)
def __init__(self):
self.continue_reading = True
def read_loop(self):
while self.continue_reading:
richter_str = input("Enter a Richter scale value from 1 to 10: ")
richter = float(richter_str)
if richter < 1.0 or richter > 10.0 :
print "You have entered an incorrect number. Please try again"
continue
energy = self.calc_energy(richter)
TNT = self.calc_TNT(energy)
print "For a Richter scale value of %.2f, the energy equivalent in Joules is: %.2f" % (richter, energy)
print "THATS %.2f tonnes of TNT!!!" % TNT
def calc_energy(self, richter):
energy = 10**(1.5 * richter + 4.8)
return energy
def calc_TNT(self, energy):
TNT = energy/self.tnt_joule_ratio
return TNT
richt = stricter_richter()
richt.read_loop()