自学之后运用循环语句和判断语句所解决的几个简单问题:
1、实现1到10的和:
x = 1
he = 0
while x < 11:
if x == 7:
pass
else:
he = he + x
x = x + 1
print(he)
2、实现1到100的和:
x=1
he =0
while x< 101:
he=x+he
x=x+1
print(he)
3、实现100以内偶数相加:
x = 1
while x < 101:
if x % 2 == 0:
print(x)
else:
pass
x = x + 1
4、实现100以内的奇数相加:
x = 1
while x < 101:
if x % 2 == 0:
pass
else:
print(x)
x = x + 1
5、实现求1-2+3-4+5 ... 99的所有数的和:
x = 1
he = 00
while x < 100:
if x % 2 == 0:
he = he - x
else:
he = he + x
x = x + 1
print(he)
6、实现三次登陆尝试:
纯IF语句版:
idcard = 123
passward = 123
x = 0
a = input("请输入你的账号")
c = input("请输入你的密码")
if a == idcard and c == passward:
print("成功登陆")
else:
print("登陆失败")
a = input("请输入你的账号")
c = input("请输入你的密码")
if a == idcard and c == passward:
print("成功登陆")
else:
print("登陆失败")
a = input("请输入你的账号")
c = input("请输入你的密码")
if a == idcard and c == passward:
print("成功登陆")
else:
print("三次登陆失败,强制退出")
添加 while循环版:
idcared= 123
passward = 123
x = 0
while x < 3:
a = input("请输入你的账号")
c = input("请输入你的密码")
if a == idcard and c == passward:
print("成功登陆")
else:
print("登陆失败 请重新输入")
if x==2 :
print("输入三次失败,强制退出")
x = x + 1
经过这几个问题的简单尝试总结了 1、常常会忘记添加循环的限制变量。 2、不注意代码当中的缩进量。