# 打印0-10去掉5
count = -1 while count < 10: count += 1 if count == 5: continue print(count)
# 打印0-10的偶数
count = 0 while count<11: print(count) count+=2
# while死循环
import time #引用内置函数用import,这里引用的时间函数 count = 0 while True: print(count) count+=1 time.sleep(2)
# break与continue
# break:终止循环
# continue:跳过本次循环
count = 0 while count<10: print(count) count+=1 if count==5: break
# for循环
for i in range(1,10): for j in range(1,i+1): print('%s*%s=%s'% (j,i,j*i),end=' ') print('')
# 逻辑运算
a = 1 b = 2 c = 3 d = 4 if a==2 or b==2 and c==3 or d==2: print('qwer') # 逻辑运算中同时有多个‘and’和‘or’,先看算and,再算or
#if逻辑运算练习:
age = int(input("请输入你家狗狗的年龄: ")) print("") if age < 0: print("你是在逗我吧!") elif age == 1: print("相当于 14 岁的人。") elif age == 2: print("相当于 22 岁的人。") elif age > 2: human = 22 + (age -2)*5 print("对应人类年龄: ", human) ## 退出提示 input("点击 enter 键退出")
# 计算1-100的总和
a = 0 b = 0 while a<101: b+=a a+=1 print(b)
# 1.做一个名片管理系统
# 2.名片内容有name,age,phone
# 3.可以实现增删改查功能
#made in zhou
sys=[] name = {} while True: print(sys) n = int(input('请输入1增,2删,3改,4查,5退出:')) if n==1: a = input('name:') b = input('age:') c = input('phone:') name.setdefault('name',a) name.setdefault('age',b) name.setdefault('phone',c) sys.append({'name': a, 'age': b, 'phone': c}) elif n==2: d = input('输入需要删除的名字:') for e in sys: if e['name']==d: sys.remove(e) print("已删除!") else: print('此用户不存在') # elif n==3: # f = input('') elif n==4: f = input('输入用户') for g in sys: if f in g['name']: print(g) else: continue # break print('用户不存在') elif n==5: break
#名片管理系统答案
info = [] while True: print('*****欢迎来到名片管理系统*****') print(' 1.查看名片') print(' 2.添加名片') print(' 3.删除名片') print(' 4.修改名片') print(' 5.退出系统') print('*' * 30) choose = input('请选择:').strip() #查看 if choose == '1': if info: info_lenth = len(info) for j in range(info_lenth): print('%s.姓名:%s 年纪:%s 电话:%s' % (j, info[j]['name'], info[j]['age'], info[j]['phone'])) else: print('空') #增加 elif choose == '2': name = input('name:').strip() age = input('age:').strip() phone = input('phone:').strip() if name and age and phone: info.append({ 'name': name, 'age': age, 'phone': phone }) else: print('请输入相应的信息') #删除 elif choose == '3': info_lenth = len(info) for j in range(info_lenth): print('%s.姓名:%s 年纪:%s 电话:%s' % (j, info[j]['name'], info[j]['age'], info[j]['phone'])) res = input('请选择要删除的名片:') info.remove(info[int(res)]) print('删除成功!') #修改 elif choose == '4': info_lenth = len(info) for j in range(info_lenth): print('%s.姓名:%s 年纪:%s 电话:%s' % (j, info[j]['name'], info[j]['age'], info[j]['phone'])) res2 = input('请选择要修改的内容:') edit_name = input('姓名(回车不修改):').strip() edit_age = input('年纪(回车不修改):').strip() edit_phone = input('电话(回车不修改):').strip() if edit_name: info[int(res2)]['name'] = edit_name if edit_age: info[int(res2)]['age'] = edit_age if edit_phone: info[int(res2)]['phone'] = edit_phone print('修改成功!') #退出 elif choose == '5': print('感谢使用!') break else: print('请正确选择')