1.变量
变量只能由字母、数字、下划线组成
特例:1.变量不能用数字开头
2.不能是python的关键字
3.最好不要和python内置的东西重复
让变量名有意义 些
python3的关键字如下:
False, None, True, and, as, assert, break, class, continue, def, del, elif, else, except, finally, for, from, global, if, import, in, is
lambda, nonlocal, not, or, pass, raise, return, try, while, with, yield
2.循环的例题
1.使用循环语句输出123456 89 10
n = 1
while n <=10:
if n == 7:
pass
else:
print(n)
n = n + 1
2.求1 - 100 的所有数的和
n = 1
count = 0
while n <= 100:
count = count + n
n = n +1
print(count)
3.输出1 - 100 内的所有奇数
n = 1
while n <= 100:
if n % 2 != 0:
print(n)
n = n + 1
4.输出1 - 100 内的所有偶数
n = 1
while n <= 100:
if n % 2 == 0:
print(n)
n = n + 1
5.求1-2+3-4...+99的所有数的和
n = 1
count = 0
while n <= 5:
if n % 2 == 0:
count = count - n
else:
count = count + n
n = n + 1
print(count)
6.用户登录,三次机会重试
下面代码是自己写的,还未听老师的修改
n = 3 id1 = "大白" passwd = "521" while n >= 1: user = input("请输入用户名:") userid = input("请输入密码:") if user == id1 and userid == passwd : print("登录成功") else: print("用户名或密码错误,请重新输入!") n = n - 1 print("机会用完了qaq")