-1

I am trying to make a coffee application and I have set up a function in which I can change a global variable of the amount of coffee beans however when i run my code this is not working. I will past my code and my results below.

I am wanting this to subtract from the global variables I have set: [amountOfBeans and 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 :)

I then tried this and did not work i got an error. i will leave my 2nd attempt and error bellow.

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
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Inside your function definition use global keyword for accessing global variable, Also you haven't set the result of the subtraction on the global variable. For example

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 :)")

Now call the method as FlatWhite(flatWhiteMilk, flatWhiteBeans)

Note : Your function is not thread safe since you are using global variable without exclusive access

Rajan Chauhan
  • 1,378
  • 1
  • 13
  • 32
  • I just got the error File "coffe.py", line 9 def FlatWhite(flatWhiteMilk,amountOfMilk, flatWhiteBeans,amountOfBeans): SyntaxError: name 'amountOfBeans' is local and global –  Apr 23 '19 at 20:50
  • @Luke Changed it. you don't need to pass global as args to the method. – Rajan Chauhan Apr 23 '19 at 20:53
  • Please pardon correct the `amount` spelling in your program. – Rajan Chauhan Apr 23 '19 at 20:58
  • Yes just noticed that now and I have corrected it thank you ! It is now working successfully thanks for the quick help. –  Apr 23 '19 at 21:02
0

This will work.

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)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Stefan
  • 375
  • 1
  • 9
  • 24