You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/anaconda3/bin/python3
import random
x = random.choice(range(10))
y = random.choice(range(20))
if x > y:
print('x:', x)
elif x == y:
print('x = y =', x)
else:
print('y:', y)
python3 test05.py
if - elif - else可嵌套,类似这样:
if 条件1:
语句
if 条件2:
语句
elif 条件3:
语句
else:
语句
elif 条件4:
语句
else:
一组有趣的例子:条件为真或为假
1、条件为真:不为0、为True、为”None”
>>> a = 1
>>> if a:
... print("True")
... else:
... print("False")
...
True
>>> a = "None"
>>> if a:
... print("True")
... else:
... print("False")
...
True
2、条件为假:为0、为False、为None
>>> a = 0
>>> if a:
... print("True")
... else:
... print("False")
...
False
>>> a = None
>>> if a:
... print("True")
... else:
... print("False")
...
False