逻辑判断 - 003课
用户输入判断:
1 _username='Lxxxl' 2 _password='asd123' 3 username=input("username:") 4 password=input("password:") 5 6 if _username==username and _password==password: 7 print("Welcome user {name} login...".format(name=username)) 8 else: 9 print("Invalid username or password!")
以上只能判断一次
while True: _username = 'Lxxxl' _password = 'asd123' username = input("username:") password = input("password:") if _username == username and _password == password: print("Welcome user {name} login...".format(name=username)) else: print("Invalid username or password!")
一直循环
for i in range(3): _username = 'Lxxxl' _password = 'asd123' username = input("username:") password = input("password:") if _username == username and _password == password: print("Welcome user {name} login...".format(name=username)) else: print("Invalid username or password!")
循环三次,不论用户信息输入对或错
for i in range(3): _username = 'Lxxxl' _password = 'asd123' username = input("username:") password = input("password:") if _username == username and _password == password: break print("Welcome user {name} login...".format(name=username)) else: print("Invalid username or password!")
答对结束,答错继续循环三次
1 count=0 2 while True: 3 print("count:",count) 4 count=count+1
if count==1000:
break
循环:数字从0开始+1 ,加到1000停止
1 Lxxxl_age=23 2 guess_age=int(input("Guess age:")) 3 if guess_age==Lxxxl_age: 4 print("Yse,you got it .") 5 elif guess_age>Lxxxl_age: 6 print("Think smaller...") 7 else: 8 print("Think bigger!")
猜年龄 运行一次
1 Lxxxl_age = 23 2 count=0 3 while True: 4 if count==3: 5 break 换成 while count<3: 6 guess_age = int(input("Guess age:")) 7 if guess_age == Lxxxl_age: 8 9 print("Yse,you got it .") 10 break 11 elif guess_age > Lxxxl_age: 12 print("Think smaller...") 13 else: 14 print("Think bigger!") 15 count+=1
else:
print("You have tried too many times ...fuck off")
1 for i in range(3): 2 Lxxxl_age = 23 3 guess_age = int(input("Guess age:")) 4 if guess_age == Lxxxl_age: 5 6 print("Yse,you got it .") 7 break 8 elif guess_age > Lxxxl_age: 9 print("Think smaller...") 10 else: 11 print("Think bigger!")
else:
print("You have tried too many times ...fuck off")
猜年龄 运行三次 如果答对结束(break要放在print后,否则直接结束,没有print) 两种方法, 第一种是计数,第二种属于优化。
1 for i in range(10): 2 print("loop",i) 3 4 for i in range(0,10,2): 5 print("loop",i)
for循环 第一个是默认从0开始,中间隔1个
Lxxxl_age = 23 count=0 while count<3: guess_age = int(input("Guess age:")) if guess_age == Lxxxl_age: print("Yse,you got it .") break elif guess_age > Lxxxl_age: print("Think smaller...") else: print("Think bigger!") count+=1 if count==3: countine_confirm=input("Do you want to keep guessing..?") if countine_confirm!='n': count=0
如果猜错可以选择是否继续,输入n就会结束。
LXXXI