0

Basically I would like to call the variable 'en' from the 'energy' function into the 'TNT' function. How do I go about doing that?

class richter_Stuff():
 def energy(self):
 richter = float(input("Enter a Richter scale value from 1 to 10: "))
 if richter >= 1.0 and richter <= 10.0 :
 en = 10**(1.5 * richter + 4.8)
 print "For a Richter scale value of %.2f, the energy equivalent in Joules is: %.2f" %(richter, en)
 return
 else:
 print "You have entered an incorrect number. Please try again"
 energy()
 def TNT(self, energy):
 one_tonne_tnt_to_joules = 4.184*(10**9)
 TNT = en/one_tonne_tnt_to_joules
 print TNT
TNT()
asked May 24, 2014 at 10:18
0

2 Answers 2

3

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:

  1. Read richter scale. Reread if wrong.

  2. Calculate joules from scale.

  3. Calculate TNT quantity from joules.

  4. Print information.

  5. 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()
answered May 24, 2014 at 11:19
Sign up to request clarification or add additional context in comments.

Comments

1

You can return en in energy function and assign it to some other variable in TNT or use globals or since you are using classes you can also use self.en as variable name instead of just en to point out that en is in that class' scope.

Probably best one in this case is using advatages of class.

class richter_Stuff():
 def energy(self):
 richter = float(input("Enter a Richter scale value from 1 to 10: "))
 if richter >= 1.0 and richter <= 10.0 :
 self.en = 10**(1.5 * richter + 4.8)
 print "For a Richter scale value of %.2f, the energy equivalent in Joules is: %.2f" %(richter, self.en)
 else:
 print "You have entered an incorrect number. Please try again"
 self.energy()
 def TNT(self):
 en = self.en
 one_tonne_tnt_to_joules = 4.184*(10**9)
 TNT = en/one_tonne_tnt_to_joules
 print TNT
richt = richter_Stuff()
richt.energy()
richt.TNT()

You can call that variable outside of your class using richt.en.

Also, using recursion might not be your best shot here. If user inputs lots of wrong entries, your program will give an error about maximum recursion depth. You should change your asking input style according to this answer.

answered May 24, 2014 at 10:24

1 Comment

@dilbert Ah, ofcourse. I was just answering this particular question. Adding another answer's link to get rid of recursion since it explained much better than I ever can.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.