[英]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.