输入某年某月某日,判断这一天是这一年的第几天?
实例(Python 2.0+)#!/usr/bin/python
# -*- coding: UTF-8 -*-
year = int(raw_input('year:\n'))
month = int(raw_input('month:\n'))
day = int(raw_input('day:\n'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months
else:
print 'data error'
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print 'it is the %dth day.' % sum
------------
这个题目也有个问题,就是在输入月份之后输入日期时,倘若日期不对,也得报错,但我想了一顿也不知应该如何写?求高手指教。 不玩 Python 2.x。 zltzlt 发表于 2019-12-10 21:03
不玩 Python 2.x。
year = int(input('year:\n'))
month = int(input('month:\n'))
day = int(input('day:\n'))
months = (0,31,59,90,120,151,181,212,243,273,304,334)
if 0 < month <= 12:
sum = months
else:
print( 'data error')
sum += day
leap = 0
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
if (leap == 1) and (month > 2):
sum += 1
print ('it is the %dth day.' % sum)
给您改成3了。请指教。另:您的每日一题中鱼友“冬雪雪冬”给的答案里“str2 += f'{char}{count}'”是啥意思? zltzlt 发表于 2019-12-10 21:03
不玩 Python 2.x。
俺就是在研究Python100例,但菜鸟教程上确实用的是2.0,俺 也觉着别扭。求大神指导。 修改一个:
months = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
months_leap = (31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
while True:
year = int(input('year:\n'))
if 0 < year < 9999:
break
else:
print('year error.')
while True:
month = int(input('month:\n'))
if 0 < month < 13:
break
else:
print('month error.')
if (year % 400 == 0) or ((year % 4 == 0) and (year % 100 != 0)):
leap = 1
else:
leap = 0
while True:
day = int(input('day:\n'))
if leap:
if 0 < month <= months_leap:
break
else:
if 0 < month <= months:
break
print('day error.')
if leap:
days = sum(months_leap[:month - 1]) + day
else:
days = sum(months[:month - 1]) + day
print ('it is the %dth day.' % days) c870801 发表于 2019-12-10 21:19
给您改成3了。请指教。另:您的每日一题中鱼友“冬雪雪冬”给的答案里“str2 += f'{char}{count}'”是啥意 ...
这是python3.6新增的f字符串f-String
f'{char}-{count}'相当于
'%s-%s'%(char, count) 冬雪雪冬 发表于 2019-12-10 21:52
这是python3.6新增的f字符串f-String
f'{char}-{count}'相当于
'%s-%s'%(char, count)
多谢。 冬雪雪冬 发表于 2019-12-10 21:50
修改一个:
只是第22行和25行的month应改为day。
再问一句,怎么出来这种带序号并可以复制代码的回答样式呢? c870801 发表于 2019-12-11 17:41
只是第22行和25行的month应改为day。
再问一句,怎么出来这种带序号并可以复制代码的回答样式呢?
发帖或回帖时注意文字上面有一个<>的图标,将程序放到弹出的窗口中。
页:
[1]