11-条件表达式、三元运算符
a = 10
b = 20
if a < b:
smaller = a
else:
smaller = b
print(smaller)
s = a if a < b else b # 和上面的if-else语句等价
print(s)
12-判断练习:用户名和密码是否正确
import getpass # 导入模块
username = input('username: ')
# getpass模块中,有一个方法也叫getpass
password = getpass.getpass('password: ')
if username == 'bob' and password == '123456':
print('Login successful')
else:
print('Login incorrect')
13-猜数:基础实现
import random
num = random.randint(1, 10) # 随机生成1-10之间的数字
answer = int(input('guess a number: ')) # 将用户输入的字符转成整数
if answer > num:
print('猜大了')
elif answer < num:
print('猜小了')
else:
print('猜对了')
print