Python - 使用决策语句和 return 变量定义函数

Python - define function using decision statement and return variable

我是 Python 的新手,我正在编写一个程序,其中涉及定义我自己的函数来确定客户是否收到他们的机票折扣。首先,我询问他们是否预注册,然后调用我的函数,该函数有一个 if-then-else 决策语句,它要么应用 10% 的折扣,要么不应用。我不确定我的程序做错了什么,有什么建议吗? 编辑:我现在的输出 returns 0$,我希望如果没有预注册则输出 20$ 或如果预注册则计算 20$ 的 10%。

def determineDiscount(register, cost):
#Test if the user gets a discount here. Display a message to the screen either way.
    if register == "YES":
        cost = cost * 0.9
        print("You are preregistered and qualify for a 10% discount.")
    else:
        print("Sorry, you did not preregister and do not qualify for a 10% discount.")
    return cost

#Declarations
registered = ''
cost = 0
ticketCost = 20 

registered = input("Have you preregistered for the art show?")
determineDiscount(registered, ticketCost)

print("Your final ticket price is", cost, "$")
def determineDiscount(register, cost):
#Test if the user gets a discount here. Display a message to the screen either way.
    if register.lower() == "yes": #make user's input lower to avoid problems
        cost -= cost * 0.10 #formula for the discount
        print("You are preregistered and qualify for a 10% discount.")
    else:
        print("Sorry, you did not preregister and do not qualify for a 10% discount.")
    return cost
#Declarations
ticketCost = 20
#get user input
registered = input("Have you preregistered for the art show?")
#call function in your print()
print("Your final ticket price is $", determineDiscount(registered, ticketCost))

输出:

Have you preregistered for the art show?yes
You are preregistered and qualify for a 10% discount.
Your final ticket price is  

代码应该在 PY2 中使用 raw_input,在 PY3 中使用 input。 此外,函数返回的成本必须存储在成本中,否则它将保持不变。函数外的成本与函数内的成本不同。

def determineDiscount(register, cost):
    if register.lower() == "yes":
        cost *= 0.9
        print("You are preregistered and qualify for a 10% discount.")
    else:
        print("Sorry, you did not preregister and do not qualify for a 10% discount.")
    return cost


ticketCost = 20
registered = raw_input("Have you preregistered for the art show?")
cost = determineDiscount(registered, ticketCost)

print("Your final ticket price is", cost, "$")