判断用户输入的账户名和密码与已知的账户名和密码是否一致:
1 #Author:Archer Zon 2 3 _username = 'archerzon' 4 _password = '1234' 5 username = input("username:\n") 6 password = input("password:\n") 7 # passoword = getpass.getpass("password:\n") 8 9 if _username == username and _password == password: 10 print("Welcome user {name} login...".format(name = username)) 11 else: 12 print("Invalid username or password!")
猜年龄:
1 #Author:Archer Zon 2 age_of_archerzon = 22 3 4 guess_age = int(input("guess age:")) 5 6 if guess_age == age_of_archerzon: 7 print("Yes,you got it!") 8 elif guess_age > age_of_archerzon: 9 print("think smaller") 10 else: 11 print("think bigger!")
小结:pytho 3.x里面 input 默认输入的是 str 。当判断整数型时,需要在 input 前面加 int,否则会报错。
guess_age = int(input("guess age:"))