繁体   English   中英

有人可以告诉我我做错了什么[暂停]

[英]Can someone tell me what I'm doing wrong [on hold]

无论我改变多少,我似乎都无法让它发挥作用。 编码:

def main():
    #define age variable
    def getAge(): #ask for the age of the customer
        getAge() = 0
    def fullPrice(): #Full price of ticket for park
        fullPrice() = 25
    def useDiscount(): #appropriate discount will be applied to customer based on age
        useDiscount() = 0
print(float(input("Customer is how old? ")))

if age < 7
    useDiscount() == .30(fullPrice())
elif 7 < age < 14
    useDiscount() == .20(fullPrice())
elif age > 14 and age < 20
    useDiscount() == .25(fullPrice())
elif age > 20 and age < 60
    useDiscount() == .15(fullPrice())
else:
    useDiscount() == .40(fullPrice())

main()

您在函数和变量之间混合。 函数是可调用的(我们可以使用 () 来调用它们)而变量不是。 你必须把age作为一个变量。 IE,

age = int(input("Customer is how old? "))

折扣可以是一个变量。 fullPrice()可以是这样的:

def fullPrice(): #Full price of ticket for park
    return 25

您没有在任何地方使用getAge() if 和 elif 语句最后应该有:

if age < 7:
    discount = .30(fullPrice())
elif age > 7 and age < 14:
    discount = .20(fullPrice())
elif age > 14 and age < 20:
    discount = .25(fullPrice())
elif age > 20 and age < 60:
    discount = .15(fullPrice())
else:
    discount = .40(fullPrice())

这可能是一个好的开始:

def get_discount(age):
    if age < 7:
        useDiscount = .30
    elif 7 < age < 14:
        useDiscount = .20
    elif 14 < age < 20:
        useDiscount = .25
    elif 20 < age < 60:
        useDiscount = .15
    else:
        useDiscount = .40
    return useDiscount


if __name__ == '__main__':
    age = int(input("Customer is how old? "))
    print(get_discount(age))

我给你一些注释,你的代码有什么问题:

def main():
    #define age variable
    def getAge():
        getAge() = 0  # you assign 0 to the function getAge() -> invalid
        getAge = 0  # maybe you wanted that
    def fullPrice():
        fullPrice() = 25  # you assign 25 to the function fullPrice() -> invalid
    def useDiscount():
        useDiscount() = 0  # you assign 0 to the function useDiscount() -> invalid

# most likely an indentation error from here to excluding main()
print(float(input("Customer is how old? ")))

if age < 7  # : missing for signing the end of the expression
    useDiscount() == .30(fullPrice())  # test if the result of useDiscount() is equal to an invalid expression -> invalid
    useDiscount = .30 * fullPrice  # is what you want, i guess
elif 7 < age < 14  # : missing for signing the end of the expression
    useDiscount() == .20(fullPrice())  # test if the result of useDiscount() is equal to an invalid expression -> invalid
elif age > 14 and age < 20  # : missing for signing the end of the expression
    useDiscount() == .25(fullPrice())  # test if the result of useDiscount() is equal to an invalid expression -> invalid
elif age > 20 and age < 60  # : missing for signing the end of the expression
    useDiscount() == .15(fullPrice())  # test if the result of useDiscount() is equal to an invalid expression -> invalid
else:
    useDiscount() == .40(fullPrice())  # test if the result of useDiscount() is equal to an invalid expression -> invalid

main()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM