wheil循环 for循环
wheil循环 登录密码用户只能输入3次
counter = 0 "定义开始次数"
_user = "wl"
_password = "aaa123"
while counter <3:
username = input("Username:")
password = input("password:")
if username == _user and password == _password:
print("wel come %s login" % _user)
break # 跳出 中断
else:
print("输入错误")
counter += 1
else:
print("超过3次输入")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用户输入3次错误可以按Y继续
counter = 0
_user = "wl"
_password = "aaa123"
while counter <3:
username = input("Username:")
password = input("password:")
if username == _user and password == _password:
print("wel come %s login" % _user)
break # 跳出 中断
else:
print("输入错误")
counter += 1
if counter ==3:
keep = input("继续请按Y")
if keep == "y":
counter=0
else:
print("超过3次输入")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
布尔运算
# (True) and (False) and 左右两边执行结果都是True 才为True真
# (True) or (False) or左右两边执行结果任意结果是True真 就为真
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
age= 50
flag= True
while flag:
user_input_age = int(input("请输入年龄:"))
if user_input_age == age:
print("输入正确")
elif user_input_age> age:
print("输入年龄过大")
else:
print("你输入年龄小于")
print("111")
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~