Python第三周第一次作业

本文通过实例演示了Python中if语句的应用及如何根据条件判断进行不同逻辑处理的过程。同时,还介绍了如何利用列表来管理和检查用户的偏好水果,并展示了基于用户身份的不同问候方式。

学习if语句的使用

5-5 外星人颜色

alien_color = "green"
print("alien_color is " + alien_color +".")
if alien_color == "green":
    print("You get 5 scores!")
elif alien_color == "yellow":
    print("You get 10 scores!")
else:
    print("You get 15 scores!")

alien_color = "yellow"
print("\nalien_color is " + alien_color +".")
if alien_color == "green":
    print("You get 5 scores!")
elif alien_color == "yellow":
    print("You get 10 scores!")
else:
    print("You get 15 scores!")

alien_color = "red"
print("\nalien_color is " + alien_color +".")
if alien_color == "green":
    print("You get 5 scores!")
elif alien_color == "yellow":
    print("You get 10 scores!")
else:
    print("You get 15 scores!")

5-7 喜欢的水果

favorite_fruits = ["Apple", "Pear", "Peach"]
print("Do you like apple?")
if "Apple" in favorite_fruits:
    print("Yes!\nYou really like apple!")
print("Do you like banana?")
if "Banana" in favorite_fruits:
    print("Yes!\nYou really like banana!")
print("Do you like pear?")
if "Pear" in favorite_fruits:
    print("Yes!\nYou really like pear!")
print("Do you like melon?")
if "Melon" in favorite_fruits:
    print("Yes!\nYou really like melon!")
print("Do you like orange?")
if "Orange" in favorite_fruits:
    print("Yes!\nYou really like orange!")

5-8 打招呼

users = ["admin", "Alice", "Bobby", "Cathy", "David"]
for user in users:
    if user == 'admin':
        print("Hello admin, would you like to see a status report?")
    else:
        print("Hello " + user + ", thank you for logging in.")

5-11 序数

习惯性地将条件括起来了,但发现if用不用括号都可以……

number = range(1, 10)
for num in number:
    if(num == 1):
        print("1st")
    elif(num == 2):
        print("2nd")
    elif(num == 3):
        print("3rd")
    else:
        print(str(num) + "th")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值