鱼C论坛

 找回密码
 立即注册
查看: 56|回复: 2

[学习笔记] 我的python笔记(2025.12.23)-2

[复制链接]
发表于 昨天 14:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#第六题
#here = there = that = 1024
#print('变量here在内存中的地址:',id(here))
#print('变量there在内存中的地址:',id(there))
#print('变量that在内存中的地址:',id(that))

#第七题
#print('     ','登高','    ','李白')
#print('风急天高猿啸哀,渚清沙白鸟飞回。')
#print('无边落木萧萧下,不尽长江滚滚来。')
#print('万里悲秋常作客,百年多病独登台。')
#print('艰难苦恨繁霜鬓,潦倒新停浊酒杯。')

#第八题
#you = str(input('是否口渴,回答是或其他任何回答:'))
#if you == '是':                                                                            #如果回答‘是’,就会喝水。
#    print('喝水')
#else:
#    print('去玩')

#第九题
#year = int(input("输入一个年份: "))
#if (year % 4) == 0:
#    if (year % 100) == 0:
#        if (year % 400) == 0:
#            print(year,"是闰年")                                                      # 是整百年但能被400整除的
#        else:
#            print(year,"不是闰年")
#   else:
#        print(year, "是闰年")                                                        # 不是整百年但能被4整除的
#else:
#    print(year,"不是闰年")

#第十题
#count = 0
#while count < 5:
#    count += 1
#    if count <5:
#        print('现在是第',count,'道,需要继续答题')
#print('小明已经完成',count,'道题目,可以玩手机了')
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 昨天 14:40 | 显示全部楼层
你的Python笔记记录了一些基础代码,我来逐一解释并指出可以改进的地方。

第6题:多重赋值与对象引用

  1. here = there = that = 1024
  2. print('变量here在内存中的地址:',id(here))
  3. print('变量there在内存中的地址:',id(there))
  4. print('变量that在内存中的地址:',id(that))
复制代码


解释:三个变量同时赋值为1024,它们会引用同一个整数对象,因此 id() 的输出相同。但要注意,Python 对小整数(通常是 -5 到 256)有缓存,1024 可能不在缓存范围,不过由于多重赋值,它们仍然指向同一个对象。

第7题:打印诗歌(注意作者错误)

  1. print('     ','登高','    ','李白')
  2. print('风急天高猿啸哀,渚清沙白鸟飞回。')
  3. print('无边落木萧萧下,不尽长江滚滚来。')
  4. print('万里悲秋常作客,百年多病独登台。')
  5. print('艰难苦恨繁霜鬓,潦倒新停浊酒杯。')
复制代码


解释:这段代码打印杜甫的《登高》,但作者误写为李白。第一行使用空格对齐,效果可能不理想。建议使用字符串格式化改进,例如:

  1. print('登高'.center(20))
  2. print('杜甫'.center(20))
  3. print('风急天高猿啸哀,渚清沙白鸟飞回。')
  4. print('无边落木萧萧下,不尽长江滚滚来。')
  5. print('万里悲秋常作客,百年多病独登台。')
  6. print('艰难苦恨繁霜鬓,潦倒新停浊酒杯。')
复制代码

第8题:简单的条件判断

  1. you = str(input('是否口渴,回答是或其他任何回答:'))
  2. if you == '是':
  3.     print('喝水')
  4. else:
  5.     print('去玩')
复制代码


解释:input() 本身返回字符串,无需再用 str() 转换。考虑用户可能输入空格或不同表达,可以优化为:

  1. you = input('是否口渴,回答是或其他任何回答:').strip()
  2. if you == '是':
  3.     print('喝水')
  4. else:
  5.     print('去玩')
复制代码

第9题:闰年判断(注意缩进)


你的代码缩进有误,修正后如下:

  1. year = int(input("输入一个年份: "))
  2. if (year % 4) == 0:
  3.     if (year % 100) == 0:
  4.         if (year % 400) == 0:
  5.             print(year, "是闰年")  # 整百年能被400整除
  6.         else:
  7.             print(year, "不是闰年")
  8.     else:
  9.         print(year, "是闰年")  # 能被4整除但不能被100整除
  10. else:
  11.     print(year, "不是闰年")
复制代码


更简洁的写法:

  1. year = int(input("输入一个年份: "))
  2. if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
  3.     print(year, "是闰年")
  4. else:
  5.     print(year, "不是闰年")
复制代码

第10题:不完整的 while 循环


你的代码不完整,可能想演示 break 或 continue。例如:

  1. count = 0
  2. while count < 5:
  3.     count += 1
  4.     if count == 3:
  5.         continue  # 跳过3
  6.     print(count)
复制代码


或者:

  1. count = 0
  2. while count < 5:
  3.     count += 1
  4.     if count == 3:
  5.         break  # 提前结束循环
  6.     print(count)
复制代码


继续坚持写笔记,这对学习很有帮助!如果还有疑问,欢迎随时提出。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 昨天 17:31 | 显示全部楼层
加油
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-24 19:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表