如何修改Python while倒计时代码实现接收星期输入后倒计时到周末
实现代码
你可以直接使用下方修改后的代码,逻辑和你原有while循环的写法保持一致:
# 按顺序定义周一到周五的工作日列表 week_days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"] # 获取用户输入,去除前后空格并统一格式 input_day = input("请输入当前星期几(如Tuesday):").strip().capitalize() # 校验输入合法性 if input_day not in week_days: print("输入不合法,请输入Monday到Friday之间的有效星期") else: # 定位输入日期在列表中的起始位置 current_index = week_days.index(input_day) # while循环遍历输出 while current_index < len(week_days): print(week_days[current_index]) current_index += 1 print("It's the weekend!")
效果验证
当你输入Tuesday时,运行输出如下:
Tuesday Wednesday Thursday Friday It's the weekend!
完全符合你需要的效果。如果不需要输入校验,去掉对应的if判断分支即可。
内容的提问来源于stack exchange,提问作者Good_soup
相关产品推荐
相关产品推荐

