本章介绍关系运算符和布尔表达式,并展示如何使用选择结构控制程序流程,涉及if、if-else和if-elif-else语句,还讨论了嵌套选择结构和逻辑运算符。本章最后讨论了如何使用选择结构测试机器龟的状态。
#1一周七天
n=int(input("Please enter an integer between 1 and 7:"))
if n>=1 and n<=7:
if n==1:
print("Monday")
elif n==2:
print("Tuesday")
elif n==3:
print("Wednesday")
elif n==4:
print("Thursday")
elif n==5:
print("Friday")
elif n==6:
print("Saturday")
else:
print("Sunday")
else:
print("Get wrong!")
#2矩形面积
m=int(input("Please enter the height of the first rectangle:"))
n=int(input("Please enter the width of the first rectangle:"))
m1=int(input("Please enter the height of the second rectangle:"))
n1=int(input("Please enter the width of the second rectangle:"))
if m*n==m1*n1:
print("Both are equal in area")
elif m*n<m1*n1:
print("The second is bigger")
else:
print("The first is bigger")
#3年龄分类器
n=int(input("Please enter a person's ago:"))
if n<=1:
print("Is baby")
elif n>1 and n<13:
print("Is child")
elif n>=13 and n<20:
print("Is puber")
else:
print("Is adult")
#4罗马数字
n=int(input("Please enter an integer between 1 and 10:"))
if n<1 or n>10:
print("Get wrong")
else:
if n==1:
print("Ⅰ")
elif n==2:
print("Ⅱ")
elif n==3:
print("Ⅲ")
elif n==4:
print("Ⅳ")
elif n==5:
print("Ⅴ")
elif n==6:
print("Ⅵ")
elif n==7:
print("Ⅶ")
elif n==8:
print("Ⅷ")
elif n==9:
print("Ⅸ")
else:
print("Ⅹ")
#5质量与重量
mass=float(input("Please enter the mass of an object:"))
weight=9.8*mass
if weight>500:
print("It's too heavy!")
elif weight<100:
print("It's too light!")
else:
print("Weight:",weight,"N")
#6神奇的日期
month=int(input("Please enter month:"))
day=int(input("Please enter day:"))
year=int(input("Please enter year:"))
year=year%100
if month*day==year:
print("It's a special day!")
else:
print("It's a normal day.")
#7颜色混合器
c1=input("Please enter a primary color:")
if c1!="red" and c1!="blue" and c1!="yellow":
print("Get wrong")
exit()
c2=input("Please enter a primarg color again:")
if c2!="red" and c2!="blue" and c2!="yellow":
print("Get wrong")
exit()
if (c1=="red" and c2=="blue") or (c2=="red" and c1=="blue"):
print("Mixed result is purple")
elif (c1=="red" and c2=="yellow") or (c2=="red" and c1=="yellow"):
print("Mixed result is orange")
elif (c1=="blue" and c2=="yellow") or (c2=="blue" and c1=="yellow"):
print("Mixed result is green")
#8热狗野餐计算器
n=int(input("Please enter the number of campers:"))
sum=0
print("The number of hot dogs for everyone:")
i=0
while i<n:
x=int(input())
sum=sum+x
i=i+1
if sum%10!=0:
res=sum//10+1
else:
res=sum/10
print("The number of hot dog bags",res)
if sum%8!=0:
res1=sum//8+1
else:
res1=sum/8
print("The number of hot dog bun bags",res1)
print("The number of remains of hot dog",res*10-sum)
print("The number of remains of hot dog bun",res1*8-sum)
#9轮盘赌的颜色
n=int(input("Please enter an integer between 0 and 36:"))
if n<0 or n>36:
print("Get wrong!")
else:
if n==0:
print("Green")
elif (n>=1 and n<=10 and n%2==1)or(n>10 and n<=18 and n%2==0)or(n>18 and n<=28 and n%2==1)or(n>28 and n<=36 and n%2==0):
print("Red")
else:
print("Black")
#10数钱游戏
a=int(input("Please enter the number of penny:"))
b=int(input("Please enter the number of nickel:"))
c=int(input("Please enter the number of dime:"))
d=int(input("Please enter the number of quarter:"))
e=0.01*a+0.05*b+0.1*c+0.25*d
if e==1:
print("Win")
elif e<1:
print("Less than $1")
else:
print("More than $1")
#11读书俱乐部购买积分点数
n=int(input("Please enter the number of books purchased this month:"))
if n>=0 and n<2:
point=0
elif n>=2 and n<4:
point=5
elif n>=4 and n<6:
point=15
elif n>=6 and n<8:
point=30
else:
point=60
print("The point of winner is",point)
#12软件销售
n=int(input("Please enter the number of you want to buy:"))
discount=0
if n>=100:
discount=0.4
elif n>=50:
discount=0.3
elif n>=20:
discount=0.2
elif n>=10:
discount=0.1
print("Amount of discount:",99*(1-discount),"The total of amount:",n*99*(1-discount))
#13航运费用
n=float(input("Please enter the weight of luggage:"))
cost=1.5
if n>10:
cost=4.75
elif n>6:
cost=4
elif n>2:
cost=3
print("The cost of luggage is $",cost)
#14体重指数
weight=float(input("Please enter the weight in pound:"))
height=float(input("Please enter the height in inch:"))
BMI=weight*703/height
print("Your BMI is",BMI)
if BMI>=18.5 and BMI<=25:
print("Your is optimum")
elif BMI<18.5:
print("You are too light")
elif BMI>25:
print("You are too heavy")
#15时间计算器
n=int(input("Please enter time seconds:"))
if n>=86400:
day=n//86400
n=n%86400
if n>=3600:
hour=n//3600
n=n%3600
if n>=60:
minute=n//60
n=n%60
print("Day:",day,"Hour:",hour,"Minute:",minute,"Second:",n)
#16二月的天数
n=int(input("Enter a year:"))
flag=False
if n%100==0 and n%400==0:
flag=True
if n%100!=0 and n%4==0:
flag=True
if flag:
print("In",n,"February has 29 days.")
else:
print("In",n,"February has 28 days.")
#17WI-FI故障诊断树
print("Reboot the computer and try to connect.")
action=input("Did that fix the problem?")
if action=="no":
print("Reboot the router and try to connect.")
action=input("Did that fix the problem?")
if action=="no":
print("Make sure the cable head connecting the router and modem is firmly inserted in the slot")
action = input("Did that fix the problem?")
if action=="no":
print("Move the router to a new location.")
action = input("Did that fix the problem?")
if action =="no":
print("Get a new router.")
#18餐厅推荐
# Vegetarian---Vegan---Gluten-Free
#Joe's Gourmet Burgers 0 0 0
#Main Street Pizza Company 1 0 0
#Corner Cafe 1 1 1
#Mama's Fine Italian 1 0 1
#The Chef's Kitchen 1 1 1
flag1=input("Is anyone in your party a vegetarian?")
flag2=input("Is anyone in your party a vegan?")
flag3=input("Is anyone in your party a gluten-free?")
if flag1=="yes" and flag2=="yes" and flag3=="yes":
print("Corner Cafe")
print("The Chef's Kitchen")
elif flag1=="yes" and flag3=="yes":
print("Corner Cafe")
print("Mama's Fine Italian")
print("The Chef's Kitchen ")
else:
if flag1=="yes":
print("Main Street Pizza Company")
print("Corner Cafe")
print("Mama's Fine Italian")
print("The Chef's Kitchen")
elif flag2=="yes":
print("Corner Cafe")
print("The Chef's Kitchen")
elif flag3=="yes":
print("Corner Cafe")
print("Mama's Fine Italian")
print("The Chef's Kitchen")