python if,循环的练习

1.变量值的交换

​    s1='alex'
​    s2='SB'

  (s1,s2) = (s2,s1)

2.有存放用户信息的列表如下,分别存放用户的名字、年龄、公司信息
userinfo={
  'name':'egon',
  'age':18,
  'company_info':{
    'cname':'oldboy',
    'addr':{
      'country':'China',
      'city':'Shanghai',
       }
         }

}


要求取出该用户公司所在的城市
print(userinfo['company_info']['addr']['city'])

3.students=[
{'name':'alex','age':38,'hobbies':['play','sleep']},
{'name':'egon','age':18,'hobbies':['read','sleep']},
{'name':'wupeiqi','age':58,'hobbies':['music','read','sleep']},
]
取第二个学生的第二个爱好
print(students[1]['hobbies'][1])

4.students=[
{'name':'egon','age':18,'sex':'male'},
{'name':'alex','age':38,'sex':'fmale'},
{'name':'wxx','age':48,'sex':'male'},
{'name':'yuanhao','age':58,'sex':'fmale'},
{'name':'liwenzhou','age':68,'sex':'male'}
]
要求循环打印所有学生的详细信息,格式如下
<name:egon age:18 sex:male>
<name:alex age:38 sex:fmale>
<name:wxx age:48 sex:male>
<name:yuanhao age:58 sex:fmale>
<name:liwenzhou age:68 sex:male>

count = 0
while count < 5:
  print("<name:%s age:%s sex:%s>" % (students[count]['name'], students[count]['age'], students[count]['sex']))
  count += 1

5.编写程序,#根据用户输入内容打印其权限

'''
egon --> 超级管理员
tom --> 普通管理员
jack,rain --> 业务主管
其他 --> 普通用户
'''
name = input("please input your username:")
if name == 'egon':
  print("超级管理员")
elif name == 'tom':
  print("普通管理员")
elif name == 'jack' or name == 'rain':
  print("业务主管")
else:
  print("普通用户")

6.猜年龄游戏升级版
要求:
允许用户最多尝试3次
每尝试3次后,如果还没猜对,就问用户是否还想继续玩,如果回答Y或y, 就继续让其猜3次,以此往复,如果回答N或n,就退出程序
如何猜对了,就直接退出
AGE = '18'
count = 0
while count < 3:
  guess = input("请输入要猜测的年龄值:")
  if guess == AGE:
    print("恭喜你猜对了")
    break
  else:
    print("你还剩%d次机会" % (2 - count))
  count += 1
while count == 3:
  print("玩家是否想继续游戏:")
  answer = input("<<")
  if answer == 'Y' or answer == 'y':
    count = 0
  elif answer == 'N' or answer == 'n':
    count = 4    #只要置大于3就行,用来退出循环
  else:
  print('''输入错误,只能从以下选择输出
Y
y
N
n
''')

猜你喜欢

转载自www.cnblogs.com/szx0608/p/9991223.html
今日推荐