为什么while循环不通过循环,它在前3个输入后停止?

时间:2017-12-04 05:08:11

标签: python python-2.7 while-loop

我正在尝试让这个程序来计算这些电影会给客户带来多少费用,但是while循环只询问前三个问题,它从不打印总额,税收或其他任何东西。请帮我解决这个问题!

def checkout():
    The_Bourne_Identity = 1
    Harry_Potter = 2
    Holy_Cross = 3
    Arrival = 4
    Hidden = 5

    price_of_movie = 0
    price_of_movie2 = 0
    price_of_movie3 = 0
    price_of_movie4 = 0
    price_of_movie5 = 0

    ans = 0
    response = 'y'

    while response == 'y':
        ans = int(input('What movie do you have?'))
        q1 = int(input('How many of these do you have'))
        response =input('Do you have a different movie?: type y for yes: ')
        if ans == The_Bourne_Identity:
            price_of_movie = 9.99 * q1  
            return price_of_movie
        elif ans == Harry_Potter:
            price_of_movie2 = 15.99 * q1
            return price_of_movie2   

        elif ans == Holy_Cross:
            price_of_movie3 = 4.75 * q1
            return price_of_movie3
        elif ans == Arrival:
            price_of_movie4 = 24.99 * q1
            return price_of_movie4 

        elif ans == Hidden:
            price_of_movie4 = 29.98 * q1
            return price_of_movie5



    movie_total_price =  price_of_movie + price_of_movie2 + price_of_movie3 + price_of_movie4 + price_of_movie5

    movie_tax = movie_total_price * .07
    total_cost = movie_tax + movie_total_price

    print("Your movies cost,", movie_total_price)
    print("Your tax will be,", movie_tax)
    print("Your total cost,", total_cost)


def main():
    checkout()
main()

当我运行它时,只需执行前3个问题并停止

2 个答案:

答案 0 :(得分:1)

你正在return中途。方法中的其余代码将不会执行。而不是return只需使用movie_total_price来计算总费用并加起来。像这样改变你的方法(可以重构更多):

def checkout():
    The_Bourne_Identity = 1
    Harry_Potter = 2
    Holy_Cross = 3
    Arrival = 4
    Hidden = 5

    price_of_movie = 0
    price_of_movie2 = 0
    price_of_movie3 = 0
    price_of_movie4 = 0
    price_of_movie5 = 0
    movie_total_price = 0
    ans = 0
    response = 'y'

    while response == 'y':
        ans = int(input('What movie do you have?'))
        q1 = int(input('How many of these do you have'))
        response =input('Do you have a different movie?: type y for yes: ')

        if ans == The_Bourne_Identity:
            movie_total_price += 9.99 * q1              
        elif ans == Harry_Potter:
            movie_total_price += 15.99 * q1
        elif ans == Holy_Cross:
            movie_total_price += 4.75 * q1            
        elif ans == Arrival:
            movie_total_price += 24.99 * q1
        elif ans == Hidden:
            movie_total_price += 29.98 * q1


    movie_tax = movie_total_price * .07
    total_cost = movie_tax + movie_total_price

    print("Your movies cost,", movie_total_price)
    print("Your tax will be,", movie_tax)
    print("Your total cost,", total_cost)

答案 1 :(得分:0)

如果任何条件为True,则从结帐功能返回。如果没有理由这样做,请注释掉return语句,你应该看到剩余的日志。