一、代码练习
第二章动手练习:
message = "2022.5.31 星期二 雨" \
"\n请继续坦坦荡荡的微笑面对五颜六色的生活" \
"\n是的,可以五颜六色" \
"\n不是乱七糟八~"
print(message)
(运行成功!)
message = "今天天气不好,所以心情也不好"
print(message)
message = "没关系,我猜是因为今天值得被记住~"
print(message)
name = "Eric"
# name = input() 与用户简单的交互
sayhi = f"Hello {name},would you like to learn some Python today?"
print(sayhi)
name = "fuYoujing"
print(name)
print(name.title())
print(name.upper())
print(name.lower())
Myfavorite = '《牧羊少年奇幻之旅》中曾说过,' \
'\n"当你真心想做一件事的时候,' \
'全宇宙都会联合起来帮助你"' \
'\n请大胆的去追寻你的热爱吧!!' \
'\n我相信你所热爱的值得你去追寻♥'
print(Myfavorite)
book = "《牧羊少年奇幻之旅》"
pick = "当你真心想做一件事的时候,全宇宙都会联合起来帮助"
hope = "请大胆的去追寻你的热爱吧!!" \
"\n我相信你所热爱的值得你去追寻♥"
message = f'{book}曾说过,\n"{pick}",\n{hope}'
print(message)
name = " \t jing jing \n"
print("未去除空白:",name)
print("去除开头空白:",name.lstrip())
print("去除末尾空白:",name.rstrip()) # 换行也被去除掉了
print("去除开头和末尾空格:",name.strip())
print("\n大功告成~")
print(2+6)
print(10-1+5-6)
print(24/3) #将任意两个数相除时,结果总是浮点数,即便这两个数都是整数且能整除
print(1*8)
print(5*8/5)
print(80%9) # 模运算,只显示余数
Ilike = int(4) # Ilike = 4 #设置我喜欢的数字
Myanswer = f"{Ilike} is my favourite number."
question = "Which number do I like? " \
"Now you can have a guess!!" \
"\nYour answer:"
print(question)
youranswer = input() # 获取用户输入答案,
youranswer=int(youranswer) # 并转换为整数
# if-else判断答案是否正确 try1
if youranswer == Ilike:
print("Oh,Congratulations!!you guessed it!")
else:
print("It's a pity.")
print(Myanswer)
# while判断答案是否正确以及是否继续猜 try2 (CTRL / 快速注释)(貌似不太会,有待完善,但成功啦)
# 当你的答案与我的答案不同时进入while循环,对你的答案进行提示
# while youranswer != Ilike:
# if youranswer < Ilike:
# print("It's a pity.Your number is below what I like. Let's guess again.")
# youranswer = input()
# youranswer = int(youranswer)
# elif youranswer > Ilike:
# print("It's a pity.Your number is beyond what I like. Let's guess again.")
# youranswer = input()
# youranswer = int(youranswer)
# 当我们答案相等时证明猜对了,也就退出了while循环,输出恭喜你猜对了
# print("Oh,Congratulations!!you guessed it!")
# 思考过程:
# 继续输入之后没办法进入下一个判断,难道是要写进一个循环?
# while循环好像是可以的!While循环不断运行直到指定条件不满足为止,而for循环用于针对集合中的每个元素都执行一个代码块。
【以上代码非官方答案,均为自己尝试,全都可以运行,欢迎指出不足】
不开心的一天,我的平板被我弟锁机了,数据全丢掉,我好难过。但是Python貌似治愈了我一点点,thanks!
Day 6
(学习中的小菜鸟,感谢指出不足,但是请轻喷,谢谢♥)