电影票:电影院根据一个人的年龄收取不同的票价.如果一个人未满3岁,则该机票是免费的; 如果他们在3到12之间,门票是10美元; 如果他们超过12岁,门票是15美元.写一个循环,在其中询问用户他们的年龄,然后告诉他们他们的电影票的成本.
我希望这个程序具有戒烟价值.我可以使用0作为我的退出值,但我想使用'退出'.
prompt = "What is your age? " prompt += "\nEnter 'quit' to close program." age = 0 while True: age = raw_input(prompt) if age == 'quit': break else: age = int(age) if age < 3: print("The movie ticket is FREE for you.") elif 3 <= age < 12: print("The movie ticket is $10 for you.") elif age >= 12: print("The movie ticker is $15 for you.")
Dan D... 6
我想指出你不需要这个else
条款.您可以在前一个缩进级别包含其余条件,因为break
退出循环而不是继续循环体的其余部分.
而不是
if age == 'quit': break else: age = int(age)
只是:
if age == 'quit': break age = int(age)
如果不清楚这里是整个程序的变化:
prompt = "What is your age? " prompt += "\nEnter 'quit' to close program." while True: age = raw_input(prompt) if age == 'quit': break age = int(age) if age < 3: print("The movie ticket is FREE for you.") elif 3 <= age < 12: print("The movie ticket is $10 for you.") elif age >= 12: print("The movie ticker is $15 for you.")
我还删除了无用的赋值:age = 0
因为该值age = raw_input(prompt)
在被读取之前被替换.
我想指出你不需要这个else
条款.您可以在前一个缩进级别包含其余条件,因为break
退出循环而不是继续循环体的其余部分.
而不是
if age == 'quit': break else: age = int(age)
只是:
if age == 'quit': break age = int(age)
如果不清楚这里是整个程序的变化:
prompt = "What is your age? " prompt += "\nEnter 'quit' to close program." while True: age = raw_input(prompt) if age == 'quit': break age = int(age) if age < 3: print("The movie ticket is FREE for you.") elif 3 <= age < 12: print("The movie ticket is $10 for you.") elif age >= 12: print("The movie ticker is $15 for you.")
我还删除了无用的赋值:age = 0
因为该值age = raw_input(prompt)
在被读取之前被替换.