Python-100-Days学习记录 第二天

Python-100-Days学习记录

原项目地址:https://github.com/jackfrued/Python-100-Days

Day2

练习

  1. 华氏温度转摄氏温度。
"""
F = 1.8C + 32
"""

f = float(input('请输入华氏温度:'))
c = (f - 32) / 1.8
print('%.1f华氏度 = %.1f摄氏度' % (f, c))

%.1f 保留一位小数的浮点数

  1. 输入圆的半径计算周长和面积。
import math

radius = float(input('input radius please: '))
perimeter = 2 * math.pi * radius
area = math.pi * radius * radius
print('周长: %.2f' % perimeter)
print('面积: %.2f' % area)
  1. 输入年份判断是不是闰年
year = int(input('input year please: '))
is_leap = (year %4 == 0 and year % 100 != 0 
            or year % 400 == 0)
print(is_leap)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Python实现万年历的代码: ```python # 定义函数,判断某一年是否为闰年 def is_leap(year): if year % 4 == 0 and year % 100 != 0 or year % 400 == 0: return True else: return False # 定义函数,计算某一年的总天数 def total_days(year): total = 0 for y in range(1900, year): if is_leap(y): total += 366 else: total += 365 return total # 定义函数,计算某一月的天数 def month_days(year, month): if month in [1, 3, 5, 7, 8, 10, 12]: return 31 elif month == 2: if is_leap(year): return 29 else: return 28 else: return 30 # 按照格式输出日历 def print_calendar(year, month): # 定义变量,记录当前日期 day = 1 # 计算当前月份的第一天是星期几 first_day = (total_days(year) + sum([month_days(year, m) for m in range(1, month)])) % 7 # 输出月份和星期几的表头 print("{0}年{1}月".format(year, month)) print("日 一 二 三 四 五 六") # 输出日历 for i in range(6): for j in range(7): if i == 0 and j < first_day or day > month_days(year, month): print(" ", end="") else: print("{0:2d}".format(day), end="") day += 1 if j < 6: print(" ", end="") print() # 测试代码 year = int(input("请输入年份:")) month = int(input("请输入月份:")) print_calendar(year, month) ``` 在运行代码时,需要输入指定的年份和月份,程序会输出该月份的日历。例如: ``` 请输入年份:2021 请输入月份:10 2021年10月 日 一 二 三 四 五 六 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ``` 这就是Python实现万年历的代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值