Skip to main content
 首页 » 编程设计

python之不使用 IF 语句

2025年05月04日14kenshinobiy

def get_weekday(d1, d2): 
    ''' (int, int) -> int 
    The first parameter indicates the current day of the week, and is in the  
    range 1-7. The second parameter indicates a number of days from the current  
    day, and that could be any integer, including a negative integer. Return  
    which day of the week it will be that many days from the current day. 
    >>> get_weekday(0,14) 
    7 
    >>> get_weekday(0,15) 
    1 
    ''' 
    weekday = (d1+d2) % 7 
    if weekday == 0: 
        weekday = 7 
    return weekday 

如何在不使用 if 语句的情况下解决这个问题?

顺便说一句,星期日是 1,星期一是 2,....星期六是 7

请您参考如下方法:

怎么样

weekday = (d1-1+d2) % 7 + 1