if...else 小练习
# 需求:猜年龄,可以让用户最多猜三次 age = 60 for i in range(3): guess = int(input("Input Age: ")) if guess > age: print("Too bigger.") elif guess < age: print("Too smaller.") else: print("You got it!") break if i+1 is 3: print("3 times lost.")
# 需求:猜年龄,每隔三次,问他一下还想不想再玩。 import sys age = 60 i = 0 while True: i += 1 guess = int(input("Input Age: ")) if guess > age: print("Too bigger.") elif guess < age: print("Too smaller.") else: print("You got it!") break if i%3 is 0: flag = input("Go on. Yes or No? ") if flag.upper() == "YES": pass elif flag.upper() == "NO": break
# 编写登陆接口: # 输入用户名密码 # 认证成功后显示欢迎信息 # 输错三次后锁定 # 重点是 *锁定* import getpass f = open("lock_user.txt", "r+") lock = f.read().strip() #So important i = 1 while True: username = input("Input your username: ") if username == lock: print("The username is locked.") break else: password = getpass.getpass("Input your password: ") if username == 'liukai' and password == 'liukai123': print('Welcome!') break else: print('Error.') i += 1 if i == 4: f.write(username + "\n") break f.close()