python基础
Posted qingmuzi
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python基础相关的知识,希望对你有一定的参考价值。
1、使用while循环输入1 2 3 4 5 6 ... 8 9 10
count = 1 while count <= 10: print(count) # count = count + 1 count += 1
2、使用while循环输入1 2 3 4 5 6 8 9 10
count = 1 while count <= 10: if count == 7: print(‘ ‘) else: print(count) count += 1
3、使用while循环输入1 2 3 4 5 6 8 9 10
方法一、
count = 0 while count < 10: count += 1 if count == 7: continue print(count)
方法二、
count = 0 while count < 10: count += 1 if count == 7: pass else: print(count)
4、输出1-100内所有的奇数
方法一、
count = 0 while count < 100: count += 1 if count % 2 == 1: print(count)
方法二、
count = 1 while count < 99: count += 2 print(count)
5、求1-2+3-4+5...+99的所有数的和
count = 1 sum = 0 while count < 100: if count % 2 == 1: sum += count else: sum -= count count += 1 print(sum)
6、用户登录(三次机会重试)
count = 0 while count < 3: name = input("请输入用户名") password = input("请输入密码") if name == ‘mahelin‘ and password == ‘250238linlin‘: print("登录成功") elif count == 2: print("登录失败") else: print("登录失败请重试") count += 1
以上是关于python基础的主要内容,如果未能解决你的问题,请参考以下文章