灵虚御风
醉饮千觞不知愁,忘川来生空余恨!

导航

 
def my_max(x,y):
if x > y:
return x
else:
return y
"""
当x大的时候返回x当y大的时候返回y
当某个条件成立做一件事,不成立做另外一件事
"""
x = 99999
y = 9898898

res = x if x > y else y

# 如果if后面的条件成立返回if前面的值 否则返回else后面的值
print(res)

"""
三元表达式固定表达式
值1 if 条件 else 值2
条件成立 值1
条件不成立 值2
"""
# x = 1
# y = 2
# m = 3
# n = 4
# res = x if x > y else (m if m >n else (...))

# 三元表达式的应用场景只推荐只有两种的情况的可能下
# is_free = input('请输入是否免费(y/n)>>>:')
# is_free = '免费' if is_free == 'y' else '收费'
# print(is_free)


# username = input('username>>>:')
# res = 'NB' if username == 'jason' else '垃圾'
# print(res)
posted on 2022-03-24 15:32  没有如果,只看将来  阅读(21)  评论(0编辑  收藏  举报