如何在函数中减去全局变量

时间:2019-04-23 20:38:31

标签: python python-2.7

我正在尝试制作咖啡应用程序,并且我已经设置了一个函数,可以在其中更改咖啡豆数量的全局变量,但是当我运行代码时,此函数无法正常工作。我将在下面粘贴代码和结果。

我希望它从我设置的全局变量中减去:[amountOfBeans和amountOfMilk]

typesOfCoffe = ["FlatWhite", "Long Black"]

amountOfBeans = 500
amountOfMilk = 500

flatWhiteMilk = 5
flatWhiteBeans = 2.5

def FlatWhite(flatWhiteMilk,amountOfMilk, flatWhiteBeans,amountOfBeans):
    if amountOfMilk > flatWhiteMilk and amountOfBeans > flatWhiteBeans :
        amountOfMilk - flatWhiteMilk
        amountOfBeans - flatWhiteBeans
        print(str(amountOfMilk))
        print(str(amountOfBeans))
    else:
        return

    print("Making Flat White :)")

def CheckCoffe(typesOfCoffe, typeOfCoffe):
    if typeOfCoffe in typesOfCoffe:
       eval(typeOfCoffe)(flatWhiteMilk, amountOfMilk, flatWhiteBeans, amountOfBeans)
    else:
       print("error")


print('What Type Of Coffe?')
typeOfCoffe = raw_input()
CheckCoffe(typesOfCoffe, typeOfCoffe)


--------Results------

What Type Of Coffee?
FlatWhite
500
500
Making Flat White :)

然后我尝试了此操作,但没有成功,我得到了一个错误。我将离开我的第二次尝试,并且出现以下错误。

typesOfCoffe = ["FlatWhite", "Long Black"]

amountOfBeans = 500
amountOfMilk = 500

flatWhiteMilk = 5
flatWhiteBeans = 2.5

def FlatWhite():
    global amountOfBeans
    global amountOfMilk
    global flatWhiteBeans
    global flatWhiteMilk
    if amountOfMilk > flatWhiteMilk and amountOfBeans > flatWhiteBeans :
        amountOfMilk - flatWhiteMilk
        amountOfBeans - flatWhiteBeans
        print(str(amountOfMilk))
        print(str(amountOfBeans))
    else:
        return

    print("Making Flat White :)")

def CheckCoffe(typesOfCoffe, typeOfCoffe):
    if typeOfCoffe in typesOfCoffe:
       eval(typeOfCoffe)()
    else:
       print("error")


print('What Type Of Coffe?')
typeOfCoffe = raw_input()
CheckCoffe(typesOfCoffe, typeOfCoffe)



What Type Of Coffe?
FlatWhite
Traceback (most recent call last):
  File "coffe.py", line 33, in <module>
    CheckCoffe(typesOfCoffe, typeOfCoffe)
  File "coffe.py", line 26, in CheckCoffe
    eval(typeOfCoffe)()
  File "coffe.py", line 14, in FlatWhite
    if amountOfMilk > flatWhiteMilk and amountOfBeans > flatWhiteBeans :
NameError: global name 'amountOfMilk' is not defined

2 个答案:

答案 0 :(得分:1)

在函数定义中,使用global关键字访问全局变量,而且您尚未在全局变量上设置减法的结果。 例如

def FlatWhite(flatWhiteMilk, flatWhiteBeans):
    global amountOfMilk
    global amountOfBeans
    if amountOfMilk > flatWhiteMilk and amountOfBeans > flatWhiteBeans :
        amountOfMilk = amountOfMilk - flatWhiteMilk
        amountOfBeans = amountOfBeans - flatWhiteBeans
        print(str(amountOfMilk))
        print(str(amountOfBeans))
    else:
        return

    print("Making Flat White :)")

现在将方法调用为 FlatWhite(flatWhiteMilk, flatWhiteBeans)

注意:由于您使用的是没有互斥权限的全局变量,因此您的函数不是线程安全的

答案 1 :(得分:1)

这将起作用。

typesOfCoffe = ["FlatWhite", "Long Black"]

amountOfBeans = 500
amountOfMilk = 500

flatWhiteMilk = 5
flatWhiteBeans = 2.5

def FlatWhite(flatWhiteMilk,amountOfMilk, flatWhiteBeans,amountOfBeans):
    if amountOfMilk > flatWhiteMilk and amountOfBeans > flatWhiteBeans :
        amountOfMilk = amountOfMilk - flatWhiteMilk
        amountOfBeans = amountOfBeans - flatWhiteBeans
        print(str(amountOfMilk))
        print(str(amountOfBeans))
    else:
        return

    print("Making Flat White :)")

def CheckCoffe(typesOfCoffe, typeOfCoffe):
    if typeOfCoffe in typesOfCoffe:
       eval(typeOfCoffe)(flatWhiteMilk, amountOfMilk, flatWhiteBeans, amountOfBeans)
    else:
       print("error")


print('What Type Of Coffe?')
typeOfCoffe = raw_input()
CheckCoffe(typesOfCoffe, typeOfCoffe)