优秀代码
程序员文章站
2022-07-10 22:26:59
1. 使用while循环输入 1 2 3 4 5 6 8 9 10 0 while count < 10: count += 1 if count == 7: print(' ') else: print(c count = 0 while count < 10: count += 1 if cou ......
1. 使用while循环输入 1 2 3 4 5 6 8 9 10
0 while count < 10: count += 1 if count == 7: print(' ') else: print(c
count = 0 while count < 10: count += 1 if count == 7: continue print(count)
2. 输出 1-100 内的所有奇数
count = 1 while count < 101: print(count) count += 2
count = 1 while count < 101: if count % 2 == 1: print(count) count += 1
3. 求1-2+3-4+5 ... -99的所有数的和
m = 0 count = 1 while count < 100: if count % 2 == 0: sum = sum - count else: sum = sum + count count += 1 print(sum)
3.1 计算 1 - 2 + 3 ... + 99 中除了88以外所有数的总和
i=1 sum=0 while i <100: if i==88: i += 1 continue if i%2!=0: sum=sum+i else: sum=sum-i i += 1 print(sum)
i=0 j=-1 sum=0 while i<99: i=i+1 if i==88: continue else: j = -j sum=sum+i*j print(sum)
4. 用户登陆(三次机会)
i = 0 while i < 3: username = input('请输入账号:') password = input('请输入密码:') if username == 'zss' and password == '123': print('登录成功') break else: print('登录失败请重新登录') i += 1 else: print("滚蛋吧")
username = "zss" password = "123" i = 3 while i > 0: zh = input("请输入你的账号:") i -= 1 if zh == username: mm = input("请输入你的密码:") if mm == password: print("验证成功.正在登陆......") print('''恭喜你登陆成功! 欢迎用户进入 用户名 :%s 密码 :%s '''%(zh,mm)) break else: if i == 0: print("你的机会已经没了!game over 下次见!") answer = input('再试试?Y or N') if answer == 'Y': i = 3 print("密码错误,请重新输入") print("你还有"+str(i)+"次机会") else: print("请输入正确的用户名!") if i == 0: print("你的机会已经没了!") answer = input('再试试?Y or N') if answer == 'Y': i = 3 print("你还有" + str(i) + "次机会") else: print('你TM要不要脸')
5. 格式化输出
name = input('请输入姓名:') age = input('请输入年龄:') job = input('请输入工作:') hobbie = input('你的爱好:') msg = '''------------ info of %s ----------- Name : %s Age : %d job : %s Hobbie: %s ------------- end -----------------''' %(name,name,int(age),job,hobbie) print(msg)
name = input('请输入姓名:') age = input('请输入年龄:') job = input('请输入工作:') hobbie = input('你的爱好:') msg = '''------------ info of {0} ----------- Name : {0} Age : {1} job : {2} Hobbie: {3} ------------- end -----------------''' .format(name,int(age),job,hobbie) print(msg)
6. 使用while和for循环分别打印字符串s=’asdfer’中每个元素
s = 'fkld' for i in s: print(i) index = 0 while 1: print(s[index]) index += 1 if index == len(s): break
7. 任意输入一串文字+数字 统计出来数字的个数
s = input("请输入:") # '1234324324fdsaf1fdsaf12' count = 0 for i in s: if i.isdigit(): count += 1 print(count)
8. 如用户输入:5+9或5+ 9或5 + 9,然后进行分割再进行计算
content=input('>>>').strip() con1=content.split('+') num=0 for i in con1: num+=int(i) print(num) content=input('>>>').strip() index = content.find("+") a = int(content[0:index]) b = int(content[index+1:]) print(a + b)
上一篇: 自动化测试是测试人员的遮羞布?