1.
#使用while循环输出1 2 3 4 5 6 8 9 10 count=1 while count < 11: if count == 7: count+=1 continue print(count) count+=1
2.
#输出 1-100 内的所有奇数 for j in range(1,100): if j%2==0: continue print(j)
3.
#输出 1-100 内的所有偶数 for i in range(1,51): i+=i print(i)
4.
#求1-2+3-4+5 ... 99的所有数的和 j = 1 o = 0 while j<=99: if j%2==0: o-=j else: o+=j j+=1 print(o)
5.
#用户登陆(三次机会重试 n = 0 while n<3: user=input('please enter your name:') password=input('passwd:') if user == 'buer' and password == '1234.com': print('login successful!') while True: shu = input('>>>:') if shu == 'quit': n=3 break else: print('login fail') n+=1
6.
#输入quit退出循环 while True: sentence=input('请输入内容:') if sentence == 'quit': break else: print(sentence)
7.
#求1-100相加的和 sum = 0 for x in range(101): sum+=x print(sum)
8.
#输入名字 name = input('please your name:') print('hello,',name)
9.
#求奇数和偶数的和 j = 0 o = 0 while j<=100: if j%2==0: o+=j else: o+=j #print(j) j+=2 print('偶数求和:',o) j = 1 o = 0 while j<=100: if j%2==0: o-=j else: o+=j #print(j) j+=2 print('奇数求和:',o)
10.
#登录用户buer,密码123,进入用户输quit退出循环 tag=True while tag: name=input('please your name:') pswd=input('please your password:') if name == 'buer' and pswd == '123': print('login successful!') while tag: cmd=input('==>>:') if cmd == 'quit': tag=False continue print(cmd)
11.
#乘法口诀表 for i in range(1,10): for j in range(1,i+1): print('%s*%s=%s' %(i,j,i*j),end=' ') print()
12.
# 编写for循环,利用索引遍历出每一个字符 msg = 'hello egon 666' for x in range(len(msg)): print(msg[x], end=' ')
13.
# 编写while循环,利用索引遍历出每一个字符 msg = 'hello egon 666' count = 0 while count < len(msg): print(msg[count]) count += 1
14.
# msg='hello alex'中的alex替换成SB msg='hello alex' print(msg.replace('alex','SB'))
15.
# msg='/etc/a.txt|365|get' 将该字符的文件名,文件大小,操作方法切割出来 msg='/etc/a.txt|365|get' print(msg.split('|'))
16.
# 编写while循环,要求用户输入命令,如果命令为空,则继续输入 while True: user=input('请输入指令:') if user=='': continue else: break
17.
# 6.编写while循环,让用户输入用户名和密码,如果用户为空或者数字,则重新输入 while True: name=input('please enter your name:') passwd=input('please your password:') if name=='' or name.isdigit(): #isdigit()字符串为整数 print('用户名不能为空或者数字') continue else: break
18.
# 7.编写while循环,让用户输入内容,判断输入的内容以alex开头的,则将该字符串加上_SB结尾 while True: #第一种方法 con=input('请输入内容:') aa='alex' if con[:4]==aa[:4]: #[:4] 切片,从开头4位字符串切片 print(con,'_SB') while True: #第二种方法 msg=input('请输入内容:') if msg.startswith('alex'): #startswith() 以什么字符或字符串开头 print(msg,'_SB')
19.
# 8.1.两层while循环,外层的while循环,让用户输入用户名、密码、工作了几个月、每月的工资(整数),用户名或密码为空,或者工作的月数不为整数,或者 # 月工资不为整数,则重新输入 tag = True while tag: user = input('用户名:') passwd = input('密码:') work_mons = input('月份:') salary = input('工资:') if work_mons.isdigit() and salary.isdigit(): # isdigit()判断是否为整数 work_mons = int(work_mons) salary = int(salary) # if user=='' or passwd=='' or not isinstance(work_mons,int) or not isinstance(salary,int): # print('格式错误') if user != '' and passwd != '' and isinstance(work_mons, int) and isinstance(salary, int): print('-------------------登录成功-----------------') print('-------欢迎进入后台查询系统version:1.0-------') print('----------------1.查询总工资----------------') print('---------------2.查询用户身份---------------') print('-----------------3.退出登录-----------------') print() print() while tag: cmd = input('===>>:') if cmd == '1': print('总工资:', work_mons * salary) elif cmd == '2': if user == 'alex': print('super suer') elif user == 'yuanhao' or user == 'wupeiqi': print('normal user') else: print('unkown user') elif cmd == '3': tag = False else: print('输入错误') else: print('格式错误') continue