简体   繁体   English

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

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

I can't seem to get this to work no matter how much of it I change.无论我改变多少,我似乎都无法让它发挥作用。 The code:编码:

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()

You are mixing between functions and variables.您在函数和变量之间混合。 Functions are callable(we can use () to call them) and variables are not.函数是可调用的(我们可以使用 () 来调用它们)而变量不是。 You have to take age as a variable.你必须把age作为一个变量。 ie, IE,

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

discount can be a variable.折扣可以是一个变量。 fullPrice() can be something like this: fullPrice()可以是这样的:

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

You didn't use getAge() anywhere.您没有在任何地方使用getAge() If and elif statement should have : at the end. 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())

This could be a good start:这可能是一个好的开始:

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

I give you some notes what was wrong in your code:我给你一些注释,你的代码有什么问题:

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.

相关问题 有人可以告诉我我做错了什么吗? - Can someone tell me what am I doing wrong? 有人可以告诉我我做错了什么新到 Python - Can someone please tell me what am I doing wrong New to Python 有人可以告诉我这种结构在哪里出错吗? - Can someone tell me where I'm going wrong with this structure? 我的第一个python程序:你能告诉我我做错了什么吗? - My first python program: can you tell me what I'm doing wrong? 我不明白为什么这段代码不起作用! 有人可以告诉我我做错了吗? - I don't see why this code is not working! can someone please tell me what i am doing wrong? 有人能告诉我我做错了什么吗? 它一直说 meal_cost is not defined - Can someone tell me what im doing wrong? it keeps saying meal_cost is not defined 有人可以向我解释此Python代码中我在做什么错,并帮我弄清楚吗? - Could someone explain to me what I'm doing wrong in this Python Code and help me figure it out? 谁能告诉我我在做什么错? ast.literal_eval格式错误的节点或字符串python3 - can anyone tell me what i'm doing wrong? ast.literal_eval malformed node or string python3 有人能告诉我出了什么问题,当我运行它时,浏览器说“无法访问此站点” - Can someone tell me what's wrong, when I run it the browsers says "This site can’t be reached" 有人能告诉我我的代码有什么问题吗 - Can someone tell me what is wrong with my code
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM