python 星期转换代码
时间: 2023-10-14 21:11:38 浏览: 116
以下是将星期转换为数字的Python代码:
```python
def weekday_to_number(weekday):
weekdays = {
'Monday': 1,
'Tuesday': 2,
'Wednesday': 3,
'Thursday': 4,
'Friday': 5,
'Saturday': 6,
'Sunday': 7
}
return weekdays.get(weekday, None)
```
使用方法:
```python
weekday_to_number('Monday') # 返回1
weekday_to_number('Sunday') # 返回7
weekday_to_number('Invalid') # 返回None
```
以下是将数字转换为星期的Python代码:
```python
def number_to_weekday(number):
weekdays = {
1: 'Monday',
2: 'Tuesday',
3: 'Wednesday',
4: 'Thursday',
5: 'Friday',
6: 'Saturday',
7: 'Sunday'
}
return weekdays.get(number, None)
```
使用方法:
```python
number_to_weekday(1) # 返回'Monday'
number_to_weekday(7) # 返回'Sunday'
number_to_weekday(0) # 返回None
```
阅读全文