简体   繁体   中英

Calling variable from one function into another function

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()

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()

You can return en in energy function and assign it to some other variable in TNT or use global s 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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM