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数字转为int,split
?a.m.与p.m.的进阶不太会