【Python 3.7】电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费; 3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,指出其票价。

    xiaoxiao2022-07-02  206

    【Python 3.7】电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费; 3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,指出其票价。

    题目:电影票:有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费; 3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,指出其票价。 程序1为(使用 break 语句在用户输入 ‘quit’ 时退出循环):

    prompt = "\nPlease enter the your age:" prompt += "\n(Enter 'quit' when you are finished.) " while True : age = input(prompt) if age == 'quit': break else: age=int(age) if age<3: print("free") elif (age>=3 and age<=12): print("The ticket price is 10 dollars.") else: print("The ticket price is 15 dollars.")

    程序2为(使用变量 active 来控制循环结束的时机):

    prompt = "\nPlease enter the your age:" prompt += "\n(Enter 'quit' when you are finished.) " active=True while active : age = input(prompt) if age == 'quit': active=False else: age=int(age) if age<3: print("free") elif (age>=3 and age<=12): print("The ticket price is 10 dollars.") else: print("The ticket price is 15 dollars.")

    输入2时,结果为:

    Please enter the your age: (Enter 'quit' when you are finished.) 2 free

    输入5时,结果为:

    Please enter the your age: (Enter 'quit' when you are finished.) 5 The ticket price is 10 dollars.

    输入45时,结果为:

    Please enter the your age: (Enter 'quit' when you are finished.) 45 The ticket price is 15 dollars.

    输入‘quit’时(终止程序),结果为:

    Please enter the your age: (Enter 'quit' when you are finished.) quit Process finished with exit code 0
    最新回复(0)