Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在不使用return的情况下从一个函数中获取信息以在python中的另一个函数中使用? defind(): galos=eval(输入(“起始整数是什么?”\n)) galoe=eval(输入(“结束整数是什么?\n”)) 用法=伽罗-伽罗 圆形(使用,1) 如果(使用4000000)和(使用量_Python_Python 3.x_Python 3.6 - Fatal编程技术网

如何在不使用return的情况下从一个函数中获取信息以在python中的另一个函数中使用? defind(): galos=eval(输入(“起始整数是什么?”\n)) galoe=eval(输入(“结束整数是什么?\n”)) 用法=伽罗-伽罗 圆形(使用,1) 如果(使用4000000)和(使用量

如何在不使用return的情况下从一个函数中获取信息以在python中的另一个函数中使用? defind(): galos=eval(输入(“起始整数是什么?”\n)) galoe=eval(输入(“结束整数是什么?\n”)) 用法=伽罗-伽罗 圆形(使用,1) 如果(使用4000000)和(使用量,python,python-3.x,python-3.6,Python,Python 3.x,Python 3.6,我正在编写一个创建“水费单”的基本代码。我试图在bill()中使用galos,但我不断遇到以下错误: NameError:未定义名称“galos” 我怎样才能得到那个信息 使用globals(我不建议这样) 我建议您也看看类编写函数有两个原因。一个是执行某些操作(例如print()),另一个是计算值并返回它们 你说你不想使用return,但是在一个不想使用return的函数中,没有什么意义 在函数的末尾添加 # Start script globals galos, galoe, use, fi

我正在编写一个创建“水费单”的基本代码。我试图在
bill()
中使用
galos
,但我不断遇到以下错误:

NameError:未定义名称“galos”

我怎样才能得到那个信息

使用globals(我不建议这样)


我建议您也看看类

编写函数有两个原因。一个是执行某些操作(例如
print()
),另一个是计算值并返回它们

你说你不想使用
return
,但是在一个不想使用
return
的函数中,没有什么意义

在函数的末尾添加

# Start script
globals galos, galoe, use, fin


def ind():
    globals galos, galoe, use, fin
    galos = eval(input("What was the starting integer??\n"))
    ...
像这样调用
ind()

return code, galos, galoe, use, fin
并在
print()
调用中使用这些返回值


您可以为此使用全局变量,这将在玩具程序中起作用,但有充分的理由不这样做,除非没有其他选择。

您可以使用一个简单的类作为数据的容器:

code, galos, galoe, use, fin = ind()

正如上面提到的其他答案一样,全局变量是您在这里的金票。 以下是实现这些功能的一种方法:

What was the starting integer??
123
What was the ending integer?
1235
Code: 5234
Beggining reading: 123
Ending reading: 1235
Gallons used: 1112
Money owed:$ 1000
defind():
全球galos,galoe,使用,fin
galos=eval(输入(“起始整数是什么?”\n))
galoe=eval(输入(“结束整数是什么?\n”))
用法=伽罗-伽罗
圆形(使用,1)
如果(使用<4000000):
鳍=1000
elif(使用量>4000000)和(使用量<10000000):
fin=2000.00
其他:
use1=使用-10000000
开始=2000.00
fin=str(开始+(0.00025*使用))
打印(“您的账单为:$”+fin)
def法案(代码):
#在此处的打印语句中使用字符串格式,
#字符串连接(“abc”+var)需要字符串类型。
打印(“代码:%i”%Code)
打印(“正在读取:%i”%galos)
打印(“结束读数:%i”%galoe)
打印(“使用加仑数:%i”%use)
打印(“欠款:$%.2f”%fin)
伽洛斯=0
伽罗=0
使用=0
fin=0
ind()
法案(1234)
还要注意上面打印语句的区别,这称为字符串格式,是我建议添加到工具包中的另一个工具

class Bill:

    def __init__(self, code):
        self.code = code
        self.galos = eval(input("What was the starting integer??\n"))
        self.galoe = eval(input("What was the ending integer?\n"))

        self.use = self.galoe - self.galos

        self.use = round(self.use, 1)

        if (self.use < 4000000):
            self.fin = 1000
        elif (self.use > 4000000) and (self.use < 10000000):
            self.fin = 2000.00
        else:
            use1 = self.use - 10000000  # not used?
            start = 2000.00
            self.fin = str(start + (0.00025 * self.use))
            print ("Your bill is:$" + self.fin)

        self.bill()  # this will call the class method 'bill' 
                     # defined below and generate the output

    def bill(self):
        print("Code:", self.code)
        print("Beggining reading:", self.galos)
        print("Ending reading:", self.galoe)
        print("Gallons used:", self.use)
        print("Money owed:$", self.fin)


if __name__ == '__main__':
    bill = Bill(5234)
What was the starting integer??
123
What was the ending integer?
1235
Code: 5234
Beggining reading: 123
Ending reading: 1235
Gallons used: 1112
Money owed:$ 1000
def ind():
    global galos, galoe, use, fin
    galos = eval(input("What was the starting integer??\n"))
    galoe = eval(input("What was the ending integer?\n"))
    use = galoe - galos
    round(use,1)
    if (use < 4000000):
        fin = 1000
    elif (use > 4000000) and (use < 10000000):
        fin = 2000.00
    else:
        use1 = use - 10000000
        start = 2000.00
        fin = str(start + (0.00025 * use))
        print ("Your bill is:$" + fin)


def bill(code):
    # Use string formatting throughout print statements here,
    # string concatenation ("abc" + var) expects a string type.
    print("Code:%i" %code)
    print("Beggining reading:%i" %galos)
    print("Ending reading:%i" %galoe)
    print("Gallons used:%i" %use)
    print("Money owed:$%.2f" %fin)

galos = 0
galoe = 0
use = 0
fin = 0

ind()
bill(1234)