Python:对传递给函数的变量所做的操作无法正常工作

Python: Operations done on variables passed into functions aren't working right

def pay(cost, selection,):
    deposit = 0
    deficit = cost - deposit
    change = deposit - cost
    print("That item will cost you $",cost,".")
    deposit = float(input("Enter your money amount (e.g. 1.5 for .50, .50 for [=11=].50, etc.):\n--\n"))
    if deposit < cost:
        while deficit >= cost:
            deficit -= float(input("Please enter an additional $" + str(deficit) + "."))
            if deficit > cost:
                print("Your change is $",change,".")
                print("Thank you for purchasing item#",selection,". Your change is $",change,".")
            returnyn = input("Would you like to make another purchase? Answer with a y or n\n")
            if returnyn == "Y" or returnyn == "y":

                return
    if deposit == cost:
            print("Thank you for purchasing item#",selection,".")
            returnyn = input("Would you like to make another purchase? Answer with a y or n\n")
            if returnyn == "Y" or returnyn == "y":
                return

    if deposit > cost:
        print("Thank you for purchasing item#",selection,". Your change is $",change,".")
        returnyn = input("Would you like to make another purchase? Answer with a y or n\n")
    if returnyn == "Y" or returnyn == "y":

        return

    else:
            exit()


def main():
    cost = 0
selection = 0
loopCount = 0
flag = 0
print("--- Welcome to the HOWE CO. VENDOTRON ---\n  --- Please make a selection below ---\n")
while loopCount < 3 or flag == 0:
    loopCount +=1
    if loopCount == 1:
        print("You have three transactions left.")
    if loopCount == 2:
        print("You have two transactions left.")
    if loopCount == 3:
        print("You have one transaction left.")
    if loopCount == 4:
        print(("Thank you for your business!"))
        break
    print ("Item#\tCost")
    print("1)\t\t.75\n2)\t\t$.75\n3)\t\t$.90\n4)\t\t$.75\n5)\t\t.25\n6)\t\t$.75\n7)\t\tExit transaction\n-----\n")
    selection = int(input("Please enter the item number:\n"))
    if selection > 6 or selection < 1:
        print("Invalid input.")
    if selection == 1:
        cost = 1.75
        x = pay(cost,selection,)
    if selection == 2:
        cost = 0.75
        x = pay(cost,selection,)
    if selection == 3:
        cost = 0.90
        x = pay(cost,selection,)
    if selection == 4:
        cost = 0.75
        x = pay(cost,selection,)
    if selection == 5:
        cost = 1.25
        x = pay(cost,selection,)
    if selection == 6:
        cost = 0.75
        x = pay(cost,selection,)
    if selection == 7:
          print(("Thank you for your business!"))
          break






main()

您好。我正在研究自动售货机程序。传递给 "pay" 函数的变量表现不稳定。例如,如果您输入的金额不足,它会要求额外输入,它会询问物品的成本,而不是成本 - 存款(赤字)。它也不会正确计算找零。例如,如果我选择项目 1(即 1.75)并支付 2 美元,它没有给我正确的找零金额,而是给我这个:

"Thank you for purchasing item# 1 . Your change is $ -1.75 ."

我假设支付功能有问题,但我不知道是什么。几个小时以来,我一直在挑剔这个。物品所需的额外金额不正确与退回的零钱数量不正确之间肯定存在联系。循环有问题吗?缩进?如果它真的很简单,请原谅我 -- 我才编程几个月,还有很多东西要学。

您计算的顺序错误。在以下代码中:

deposit = 0
deficit = cost - deposit
change = deposit - cost
print("That item will cost you $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for .50, .50 for [=10=].50, etc.):\n--\n"))
if deposit < cost:
    while deficit >= cost:

deficit 最初总是等于 cost,因为您会根据用户输入更改 deposit,但不会重新计算 deficit(这是根据deposit 初始化为 0),所以 deficit 仍然等于 cost,并且由于 deposit 从未使用过,所以 "eat" 初始不足存款而不是相信你。您需要在阅读 deposit 之后计算 deficit,而不是之前。您还需要检查赤字是 > 0,而不是 >= cost,因为您要消除短缺,而不是使短缺小于成本。

print("That item will cost you $",cost,".")
deposit = float(input("Enter your money amount (e.g. 1.5 for .50, .50 for [=11=].50, etc.):\n--\n"))
deficit = cost - deposit
change = -deficit
if deficit > 0:
    while deficit > 0:

代码可以使用很多其他清理,但这就是导致所述问题的原因。