CS50P week1 condition

条件语句

if

compare

x=int(input("x="))
y=int(input("y="))
if x<y:
    print("x is less than y")
elif x>y:
    print("x is greater than y")
else:
    print("x is equal to y")

grade

score=int(input("please input your score:"))

if score>=90 and score<=100:
    print("grade:A")
elif 80<=score<90:
    print("grade:B")
elif score>=70:
    print("grade=C")
elif score>=60:
    print("grade=D")
else:
    print("grade=E")

parity

def main():
    x=int(input("x="))
    if is_even(x)==True:
    #if is_even(x):
        print("Even")
    else:
        print("Odd")

def is_even(x):
    return x%2==0
    
main()

match

name = input("What's your name? ")
"""
if name == "Harry" or name == "Hermione" or name == "Ron": 
    print("Gryffindor")
elif name == "Draco":
    print("Slytherin")
else:
    print("Who?")
"""

match name: 
    case "Harry" | "Hermione" | "Ron":
        print("Gryffindor")
    case "Draco":
        print("Slytherin")
    case _:
        print("Who?")

作业

deep

answer=input("what's is the answer to the great question of life,the universe,and everything? ").strip().lower()
match answer:
    case "42"|"forty two"|"forty-two"|42:
        print("Yes")
    case _:
        print("No")

难点: 要把空格,随意大小写给转换掉(全部换为小写)
answer=input("what's is the answer to the great question of life,the universe,and everything? ").strip().lower()

bank

greet=input("Greeting:").strip().lower()
if greet.startswith("hello"):
    print("$0")
elif greet.startswith("h"):
    print("$20")
else :
    print("$100")

难点: 判断变量的前面几个字符
greet.startswith("hello")

extensions

file=input("File name: ").strip().lower()
if file.endswith(".gif"):
    print("image/gif")
elif file.endswith(".jpg"):
    print("image/jpeg")
elif file.endswith(".jpeg"):
    print("image/jpeg")
elif file.endswith(".png"):
    print("image/png")
elif file.endswith(".pdf"):
    print("application/pdf")
elif file.endswith(".txt"):
    name=file.split('.')[0]
    print("text/"+name)
elif file.endswith(".zip"):
    print("application/zip")
else:
    print("application/octet-stream")

难点: 判断后缀返回文件名.前面的东西)
file.endswith(".txt")
name=file.split('.')[0] 表示以.为分界,[0]表示取前面的部分

interpreter

x,y,z=input("Expression:").split(" ")
x=int(x)
z=int(z)
if y=="+":
    print(f"{x+z:.1f}")
elif y=="-":
    print(f"{x-z:.1f}")
elif y=="*":
    print(f"{x*z:.1f}")
elif y=="/":
    print(f"{x/z:.1f}")

难点: 按照空格分割表达式强行将string数字转为int保留指定位小数

注释
print(f"{x-z:.1f}")保留一位小数
x=int(x)强行将string数字转为int
x,y,z=input("Expression:").split(" ")将表达式按照空格分隔开
#保留小数 #string转int

meal

def main():
    z=convert(input("What time is it? ").strip().lower())
    if 7<=z<=8:
        print("breakfast time")
    elif 12<=z<=13:
        print("lunch time")
    elif 18<=z<=19:
        print("dinner time")
    else :
        return 0


def convert(tim):
    x=int(tim.split(':')[0])
    y=int(tim.split(':')[1])
    t=x+round(y/60,2)
    return t
    

if __name__ == "__main__":
    main()

难点: 按照时间格式转化为小数强行将string数字转为intsplit

?a.m.与p.m.的进阶不太会

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值