-1

So I'm trying to create a coffee machine program (you choose a drink, the machine checks the ingredients, the resources and the money you insert, then it dispenses the drink.) and it went okay until I got to the part where the program processes the coins and, if sufficient, adds them to the amount of money in the machine. I'm not really good with return statements and global vs local scopes, so I assume they are the issue. I tried googling, but nothing came up that actually solves the specific problem I have. This is the code for the function in question:

def process_coins(order, money):
    print("Please insert coins.")
    quarters = float(input("Quarters: ")) * quarters_value
    dimes = float(input("Dimes: ")) * dimes_value
    nickles = float(input("Nickles: ")) * nickles_value
    pennies = float(input("Pennies: ")) * pennies_value
    total = quarters + dimes + nickles + pennies
    if total < MENU[order]["cost"]:
        total = 0
        return print("Sorry, that's not enough money. Money refunded.")
    else:
        return money += total

PyCharm tells me "End of Statement expected" and "invalid Syntax" for the last line. When I remove "return" it runs but doesn't actually add the total to the money in the machine. I also tried putting brackets around money += total, which turns both problems into "unexpected expression syntax". I honestly don't understand why this doesn't work. I just want pycharm to add "total" (local scope) to "money"(global scope) and to return it so I can use it in the rest of the code. I also don't understand why this function wasn't able to work with the variable "money" before I added it as an argument in the brackets, but it was able to work with the variables "dimes_value" etc. just fine, even though those are mentioned just one line before "money" at the start of the code and aren't part of the arguments in the bracket of the function. What am I missing?

Yome
  • 3
  • 2
  • Please see https://stackoverflow.com/q/10851906/6045800 to understand about scopes and globals. You can ***read*** global variables without issues, but if you try to change them, you will just create a new local variable... Better to avoid globals and simply reassign the returned value – Tomerikoo Jan 30 '22 at 12:40
  • @tomerikoo I do understand scopes to a point. I'm just new to them and don't always catch mistakes when I make them. I already checked stackoverflow before I posted this question, but it didn't address the specific problem I had. Reading up on scopes again didn't help me, because I already knew what they do. I didn't turn a local variable into a global one. That's not what my issue was^^ I do hope you weren't the one who downvoted my question, cause that'd be quite unfair. – Yome Jan 30 '22 at 14:53

2 Answers2

1

The problem is that return can only return expressions, but an augmented assignment is a statement. This is why you get a syntax error.

For Python >= 3.8, you can use an assignment expression to overcome this:

    return (money := money + total)

* Note the necessary parenthesis.

This will both assign to money and return the value assigned to it.

But this will create a new problem: money will now be considered a local variable (because you assign to it inside the function) and you'll get a different error.

To overcome this, you will need to declare

global money

At the start of the function.

But really the best solution here is not to use global variables at all, simply return money + total, and assign the returned value in the global scope, where you call the function.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
0
if total < MENU[order]["cost"]:
    total = 0
    return print("Sorry, that's not enough money. Money refunded.")
else:
    money += total
    return money
  • Code is a lot more helpful when it is accompanied by an explanation. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please [edit] your question and explain how it answers the specific question being asked. See [answer]. – ChrisGPT was on strike Jan 30 '22 at 13:08
  • Thank you for your help, this does solve the bug issue and I found another bug in the rest of the code (the part I didn't add here) that stopped the total from being added to the "money" variable. – Yome Jan 30 '22 at 14:47
  • @Chris thanks to you as well. It does feel rather impersonal when people only post code. I'm glad it wasn't too complicated either or I would have had issues understanding it without an explanation. Thanks to both of you. – Yome Jan 30 '22 at 14:49