Python 从函数获取正确返回的问题

Python 从函数获取正确返回的问题,python,function,return,Python,Function,Return,为什么我的代码返回错误的票价?我也应该加上一个时间因素,但连这个都做不到。这就是我应该做的: """ Price of one bus ticket time 6-17, price 2.7, age 16-64 time 18-22, price 3.5, age 16-64 time 23 and 0-5, price 4, age 16-64 for ages 0-2 ticket is free at all times time 6-17, price 1

为什么我的代码返回错误的票价?我也应该加上一个时间因素,但连这个都做不到。这就是我应该做的:

"""
Price of one bus ticket
time 6-17, price 2.7, age 16-64
time 18-22, price 3.5, age 16-64
time 23 and 0-5, price 4, age 16-64
for ages 0-2 ticket is free at all times
time 6-17, price 1.7, ages 3-15 and 65 -->
time 18-22, price 2.5, ages 3-15 and 65 -->
time 23 and 0-5, price 3.0, ages 3-15 and 65 -->
"""

def calculate_ticket_price(age):
    ticket_price = 0
    while True:
        if age >= 0 or age <= 2:
            ticket_price = 1.0
        if age <= 15 or age >= 3 or age >= 65:
            ticket_price = 1.5
        if age > 15 or age < 65:
            ticket_price = 2.7

        return float(ticket_price)


def main():
    age = 5
    price = calculate_ticket_price(age)
    print(price)


if __name__ == '__main__':
    main()
“”“
一张公共汽车票的价格
时间6-17,价格2.7,年龄16-64
时间18-22,价格3.5,年龄16-64
时间23和0-5,价格4,年龄16-64
对于0-2岁的人,门票随时免费
时间6-17,价格1.7,年龄3-15岁和65岁-->
时间18-22,价格2.5,年龄3-15和65-->
时间23和0-5,价格3.0,年龄3-15和65-->
"""
def计算票价(年龄):
票价=0
尽管如此:
如果年龄>=0或年龄=65:
票价=1.5
如果年龄大于15岁或小于65岁:
票价=2.7
退票浮动(票价)
def main():
年龄=5岁
价格=计算票价(年龄)
印刷品(价格)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

我认为它会返回错误的价格,因为您使用的是
,而您需要

您的第一个if声明应该是:

if ((age >= 0) and (age <= 2)):
if (((age <= 15) and (age >= 3)) or (age >= 65)):
如果((年龄>=0)和(年龄=65)):
然后你的第三个:

if ((age > 15) and (age < 65)):
if((年龄>15岁)和(年龄<65岁)):

我认为它会返回错误的价格,因为您使用的是
,而您需要

您的第一个if声明应该是:

if ((age >= 0) and (age <= 2)):
if (((age <= 15) and (age >= 3)) or (age >= 65)):
如果((年龄>=0)和(年龄=65)):
然后你的第三个:

if ((age > 15) and (age < 65)):
if((年龄>15岁)和(年龄<65岁)):

您正在使用
或当我认为您想使用
时使用
。例如,此条件:

if age >= 0 or age <= 2:
        if age > 15 or age < 65:
            ticket_price = 2.7
任何时候年龄在65岁以下或15岁以上都会发生(这是每个数字),所以我希望你的函数总是返回2.7

编写此函数的一种更简单的方法是,遵循您尝试实现的简单年龄规则:

def calculate_ticket_price(age: int) -> float:
    if age <= 2:
        return 0.0  # infant price
    elif age <= 15:
        return 1.5  # youth price
    elif age <= 65:
        return 2.7  # adult price
    else:
        return 1.5  # senior price

当我认为您想要使用
时,您正在使用
。例如,此条件:

if age >= 0 or age <= 2:
        if age > 15 or age < 65:
            ticket_price = 2.7
任何时候年龄在65岁以下或15岁以上都会发生(这是每个数字),所以我希望你的函数总是返回2.7

编写此函数的一种更简单的方法是,遵循您尝试实现的简单年龄规则:

def calculate_ticket_price(age: int) -> float:
    if age <= 2:
        return 0.0  # infant price
    elif age <= 15:
        return 1.5  # youth price
    elif age <= 65:
        return 2.7  # adult price
    else:
        return 1.5  # senior price
def计算票价(年龄、时间):
尽管如此:
如果时间>=6或时间15或年龄<65:
票价=2.7
elif时间>=18或时间15或年龄<65:
票价=3.5
如果时间>=23或时间>=0或时间15或年龄<65:
票价=4.0
elif时间>=6或时间=65:
票价=1.7
elif时间>=18或时间=65:
票价=2.5
如果时间>=23或时间>=0或时间=65:
票价=3.0
其他:
票价=0.0
退票浮动(票价)
def main():
年龄=5岁
时间=12
价格=计算票价(年龄、时间)
印刷品(价格)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
进行了这些编辑。如果在每个>=,
def计算票价(年龄、时间)之间存在和:
尽管如此:
如果时间>=6或时间15或年龄<65:
票价=2.7
elif时间>=18或时间15或年龄<65:
票价=3.5
如果时间>=23或时间>=0或时间15或年龄<65:
票价=4.0
elif时间>=6或时间=65:
票价=1.7
elif时间>=18或时间=65:
票价=2.5
如果时间>=23或时间>=0或时间=65:
票价=3.0
其他:
票价=0.0
退票浮动(票价)
def main():
年龄=5岁
时间=12
价格=计算票价(年龄、时间)
印刷品(价格)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()

进行了这些编辑。如果每个>=之间都有和,发布代码会有点困难,但是说明应该是docstring格式的。只是为了澄清一下!您的代码是否报告错误或返回错误的价格?返回错误的价格。一开始我也有时间变量,但结果很糟糕,所以我想我应该先弄清楚。发布代码有点困难,但说明应该是docstring。只是为了澄清一下!您的代码是否报告错误或返回错误的价格?返回错误的价格。一开始我也有时间变量,但结果很糟糕,所以我想我应该先弄清楚。注意你的操作顺序…注意你的操作顺序。。。