====================================================================================================================
2018-10-16更新
3、判断输入的字符串是否为正整数
prompt = "\n请输入年龄:"
prompt +="\n输入'quit'退出程序."
message = ''
while message != 'quit':
message = input(prompt)
if message.isdigit():
age = int(message)
if age < 3:
print('岁数%s,观众免费' % (message))
elif age < 13:
print('岁数%s,票价为10$' % (message))
else:
print('岁数%s,票价为15$' % (message))
elif message == 'quit':
break
else:
print("请输入正确的年龄(正整数).")
====================================================================================================================
2、字典嵌套
citys = {
'beijing':{
'country':'china',
'population':'21,710,000',
'fact':'the capital of China',
},
'wahsington':{
'country':'american',
'population':'694,000',
'fact':'the capital of US'
},
'paris':{
'country':'france',
'population':'2,244,000',
'fact':'the capital of France'
},
}
citys['london'] = {
'country': 'britain',
'population': '8,136,000',
'fact': 'the capital of France'
}
for city,city_info in citys.items():
print('\nCity:' + city.title())
print('\tcountry:' + city_info['country'].title())
print('\tpopulation:' + city_info['population'])
print('\tfact:' + city_info['fact'])
运行结果如下:
City:Paris
country:France
population:2,244,000
fact:the capital of France
City:Beijing
country:China
population:21,710,000
fact:the capital of China
City:Wahsington
country:American
population:694,000
fact:the capital of US
City:London
country:Britain
population:8,136,000
fact:the capital of France
====================================================================================================================
1、打印时引用参数
river = {
'changjiang':'china',
'nile':'egypt',
'amazonas':'brazil'
}
for k,v in river.items():
print("The %s runs through %s." % (k.title(),v.title()))
运行结果如下:
The Amazonas runs through Brazil.
The Nile runs through Egypt.
The Changjiang runs through China.