python学习随笔2:python判断和循环
1.if-else
_username = 'heyue' _password = '1234' 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")
2.if-elif-else
_age = 56 age = int(input("guess age:")) if age == _age: print("you got it") elif age > _age: print("think bigger") else: print("think smaller")
3.while 最多猜三次
_age = 56 count = 0 while count < 3: age = int(input("guess age:")) if age == _age: print("you got it") break elif age > _age: print("think bigger") else: print("think smaller") count +=1 else: print("you have tried too much times...")
4. for 最多猜三次
_age = 56 for i in range(3): age = int(input("guess age:")) if age == _age: print("you got it") break elif age > _age: print("think bigger") else: print("think smaller") else: print("you have tried too much times...")
5.猜三次后询问是否继续
_age = 56 count = 0 while count < 3: age = int(input("guess age:")) if age == _age: print("you got it") break elif age > _age: print("think bigger") else: print("think smaller") count +=1 if count == 3: countinue_confirm = input("do you want to keep guessing...") if countinue_confirm != 'n': count = 0