if else 语句判断

判断用户输入的账户名和密码与已知的账户名和密码是否一致:

 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!")
View Code

猜年龄:

 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!")
View Code

小结:pytho 3.x里面 input 默认输入的是 str 。当判断整数型时,需要在 input 前面加 int,否则会报错。

guess_age = int(input("guess age:"))

猜你喜欢

转载自www.cnblogs.com/archerzon/p/9460240.html