7-3 10的整数倍 :让用户输入一个数字,并指出这个数字是否是10的整数倍。
number=input("Please enter a number:")
if int(number)%10:
print(number+" is not an integer multiple of 10!")
else:
print(number + " is an integer multiple of 10!")
输出:
Please enter a number:5
5 is not an integer multiple of 10!
Please enter a number:20
20 is an integer multiple of 10!
7-5 电影票 :有家电影院根据观众的年龄收取不同的票价:不到3岁的观众免费;3~12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
·在while 循环中使用条件测试来结束循环。
·使用变量active 来控制循环结束的时机。
·使用break 语句在用户输入’quit’ 时退出循环。
方法一:
number=0
#人数少于3人
while number<3:
age=input("How old are you?\n")
out="The price of your ticket is: "
age=int(age)
if age<3:
out += "Free"
elif age<12:
out += "10."
else:
out += "15."
print(out)
number = number+1
print("\n")
输出:
How old are you?
2
The price of your ticket is: Free
How old are you?
8
The price of your ticket is: 10.
How old are you?
13
The price of your ticket is: 15.
方法二:
active=True
while active:
age=input("How old are you?\n")
out="The price of your ticket is: "
if age=="quit":
active=False
else:
age=int(age)
if age<3:
out += "Free"
elif age<12:
out += "10."
else:
out += "15."
print(out)
print("\n")
输出:
How old are you?
2
The price of your ticket is: Free
How old are you?
10
The price of your ticket is: 10.
How old are you?
19
The price of your ticket is: 15.
How old are you?
quit
方法三:
while True:
age=input("How old are you?\n")
out="The price of your ticket is: "
if age=="quit":
break
else:
age=int(age)
if age<3:
out += "Free"
elif age<12:
out += "10."
else:
out += "15."
print(out)
print("\n")
输出:
How old are you?
1
The price of your ticket is: Free
How old are you?
10
The price of your ticket is: 10.
How old are you?
18
The price of your ticket is: 15.
How old are you?
quit
7-8 熟食店 :创建一个名为sandwich_orders 的列表,在其中包含各种三明治的名字;再创建一个名为finished_sandwiches 的空列表。遍历列表sandwich_orders ,对于其中的每种三明治,都打印一条消息,如I made your tuna sandwich ,并将其移到列表finished_sandwiches 。所有三明治都制作好后,打印一条消息,将这些三明治列出来。
sandwich_order=["Apple","chicken","tuna"]
finished_sandwiches=[]
while sandwich_order:
sandwich=sandwich_order[0]
print("I made your "+sandwich+" sandwich")
sandwich_order.remove(sandwich)
finished_sandwiches.append(sandwich)
print(finished_sandwiches)
输出:
I made your Apple sandwich
I made your chicken sandwich
I made your tuna sandwich
['Apple', 'chicken', 'tuna']