简体   繁体   English

从一个函数调用变量到另一个函数

[英]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. 基本上,我想将变量从“ energy”函数调用为“ TNT”函数。 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) . 要从最真实的意义上回答您的问题: energy()应该是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. 从焦耳计算TNT量。

  4. Print information. 打印信息。

  5. Repeat. 重复。

Your code should reflect this. 您的代码应反映出这一点。 If you look at stricter_richter.read_loop , that's exactly what happens. 如果您查看stricter_richter.read_loop ,那么事情就是这样。

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; 这就是为什么将energy重命名为calc_energy并将TNT重命名为calc_TNT原因,因为它们产生的输出与其输入有关; 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. 相关变量(例如richterenergyTNT )应该存在于同一范围内,因此它们在同一循环中计算。

The below builds upon Lafexlos' answer . 以下内容基于Lafexlos的答案

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. 您可以在能量函数中return en并将其分配给TNT中的其他变量使用global 或者由于您使用的是类,因此也可以使用self.en作为变量名,而不仅仅是en指出en在该类中。范围。

Probably best one in this case is using advatages of class . 在这种情况下,最好的办法是使用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 . 您可以使用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 . 您应该根据此答案更改询问输入样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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