输入2023年的一个日期 输出星期几 用if elif else 不用模块实现
时间: 2024-11-06 07:21:12 浏览: 46
要使用if-elif-else结构来计算给定日期(如2023年)的星期几,我们需要手动创建一个包含每个月天数的列表,并考虑到闰年的规则。以下是简单的Python代码示例:
```python
def get_weekday(year, month, day):
# 判断是否为闰年
is_leap = (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
# 创建月份天数表
days_in_month = [31, 29 if is_leap else 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# 根据输入的年月日检查星期几
weekday_index = sum(days_in_month[:month-1]) + day
# 星期一作为索引0,所以加7得到实际的星期几
return (weekday_index + 6) % 7
# 输入的日期,假设2023年1月1日
date = 2023, 1, 1
weekday_str = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
weekday = get_weekday(*date)
print(f"{date[0]}年{date[1]}月{date[2]}日是{weekday_str[weekday]}")
#
相关问题
输入2023年的一个日期 输出星期几 用if elif else实现
在Python中,你可以使用`datetime`模块来获取日期并确定星期几。这里是一个简单的示例,展示如何通过`if`、`elif`和`else`条件判断来实现:
```python
from datetime import datetime
def get_weekday(year, month, day):
# 创建一个datetime对象
date = datetime(year, month, day)
# 判断星期几,0代表星期一,6代表星期日
weekdays = ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六', '星期日']
weekday_num = date.weekday()
# 使用if-elif-else结构返回对应的星期几
if weekday_num == 0:
return weekdays[weekday_num]
elif weekday_num == 1:
return weekdays[weekday_num]
elif weekday_num == 2:
return weekdays[weekday_num]
elif weekday_num == 3:
return weekdays[weekday_num]
elif weekday_num == 4:
return weekdays[weekday_num]
elif weekday_num == 5:
return weekdays[weekday_num]
else: # 星期日
return weekdays[weekday_num]
# 示例输入2023年的某个日期
year = 2023
month = 1 # 注意月份从1开始计数
day = 1
print(get_weekday(year, month, day))
```
使用python代码做一个天数计算小程序 例如用户输入2023年11月1日,程序可以输出这是今年的 第305天 不要引用datetime
你可以通过编写一个简单的Python函数来实现这个功能,这里不使用内置的`datetime`模块,而是手动计算日期之间的差值。我们可以创建两个列表来存储每个月的天数以及闰年的规则。以下是实现这个功能的代码:
```python
def is_leap_year(year):
if year % 4 != 0:
return False
elif year % 100 != 0:
return True
elif year % 400 != 0:
return False
else:
return True
def days_in_month(month, year):
months_days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
if month == 2 and is_leap_year(year):
return 29
else:
return months_days[month - 1]
def calculate_days(date_string):
date_format = "%Y-%m-%d"
input_date = date_string.split("-")
year, month, day = int(input_date[0]), int(input_date[1]), int(input_date[2])
total_days = sum(days_in_month(month, y) for y in range(year, 1))
total_days += day - 1 # 减去开始的0天
return total_days
date_input = input("请输入日期(格式:YYYY-MM-DD),如2023年11月1日:")
days_since_year_start = calculate_days(date_input)
print(f"这是今年的第{days_since_year_start}天")
#
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)