7-2 餐馆订位: 编写一个程序,询问用户有多少人用餐。如果超过8人,就打印一条消息,指出没有空位;否则指出有空桌。
counts = int(input("Please tell me how many people want to eat here:\n"))
if counts > 8:
print ('No empty seats!')
else:
print ('Enough seats.')
结果:Please tell me how many people want to eat here:
8
Enough seats.
另一结果:Please tell me how many people want to eat here:
9
No empty seats!
7-3 10的整数倍: 让用户输入一个数字,并指出这个数字是否是10的整数倍。
num = int(input('Please enter the number: '))
if not num % 10 :
print ('It is 10*k, when k is integer.')
else :
print ('It is not 10*k, when k is integer.')
结果:Please enter the number: 99
It is not 10*k, when k is integer.
另一结果:Please enter the number: -10
It is 10*k, when k is integer.
7-5 电影票: 有家电影院根据观众的年龄收取不同的票价;不到3岁的观众免费;3-12岁的观众为10美元;超过12岁的观众为15美元。请编写一个循环,在其中询问用户的年龄,并指出其票价。
while True :
age = input('Please tell me the age, q to quit:\n')
if age == 'q':
break
age = int(age)
if age >= 0 and age < 3:
print ('free!')
elif age >= 3 and age <= 12:
print ('10 dollars.')
elif age > 12:
print ('15 dollars.')
结果:
Please tell me the age, q to quit:
-1
Please tell me the age, q to quit:
0
free!
Please tell me the age, q to quit:
5
10 dollars.
Please tell me the age, q to quit:
13
15 dollars.
Please tell me the age, q to quit:
99
15 dollars.
Please tell me the age, q to quit:
q