#一个简单的if语句
animals = ['dog', 'cat', 'pig', 'horse']
for pet in animals:
if pet == 'dog':
print("My favourite animals is " + pet.upper())
else:
print("Not " + pet.title())
print("---------------------------> 多个条件的情况")
#也可以检查多个条件 ---- 用and连接
i = 5
love = 2
you = 0
if i == 5 and love == 2 and you >= 0:
print("I love you!")
#也可以用 or 做 或 判断
if i == 5 or love == 6 or you > 0:
print("ditto.")
print("---------------------------> 检查特定值是否在列表中")
char = ['I', 'love', 'you', "hui"]
#在表里的情况
if 'hui' in char:
print(char)
#不在表里的情况
if 'zong' not in char:
print("Looking for...")
#布尔语句本质上就是If语句,其只有两种状态 True 或 Flase
number = 19
number > 18 #python可以直接给出真假的判断
#if else 和if elif else
Pass = 60
Good = 80
Excellence = 90
message = input("Please input your score:")
score = int(message)
if score >= Pass and score < 80:
print("Luckily,you have passed!")
elif score >= Good and score <= 90:
print("Congratulation,You got a Good in the test!")
elif score >= Excellence:
print("NB! You got a Excellence!")
else:
print("Fight again...")
皮一下😄
#上海理工大学2019考研---控制工程之来不来复试自测。
message = input("初试考了多少分啊?")
score = int (message) #注意要把输入转换成int类型
if score >= 300 and score < 350:
print("当然来复试!")
elif score < 300 and score >= 270:
print("那也得来复试!")
elif score >= 350:
print("滚...你不需要测试🙂")
elif score < 270: #最后一条或者直接是else,elif必须加上条件
print("你已经很努力了,考研不是唯一出路......")
#Pizza店 ---> 确认顾客是否要点普通Pizza(确认列表是否为空)
requested_toppings = ['mushroom', 'extra cheese', 'lemon']
ingredients = list(requested_toppings)
print("There are available ingredients: " + str(ingredients))
requested_topping = input("Please input the ingredients you want or input 0:")
if requested_topping == '0':
print("Are you sure you want a plain Pizza?")
message = input("Y or N ? ")
if message == "N":
print("Please choose again!")
if message == "Y":
print("Thanks for your patience!")
else:
print("Adding " + requested_topping +".")
print("\nFinshed your Pizza!")
#动手试一试 ---> 输出序数
number = [1, 2, 3, 4, 5, 6, 7, 8, 9]
for ordinal in number:
if ordinal == 1:
print(str(ordinal) + "st")
elif ordinal == 2:
print(str(ordinal) + "nd")
elif ordinal == 3:
print(str(ordinal) + "rd")
else:
print(str(ordinal) + "th")
Date 2018年12月28日
![](https://img.haomeiwen.com/i15126004/659da2b069beb437.jpeg)
网友评论