gpt4 book ai didi

python - 不使用 IF 语句

转载 作者:太空宇宙 更新时间:2023-11-04 07:43:21 25 4
gpt4 key购买 nike

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

关于python - 不使用 IF 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430201/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com