Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
code help
#1
Hello I've just started using python and i would love some help with this program code, i don't really know how to make it and it would be great if someone could help, thanks.


Problem 1 — Days of the year The program will ask the user for a day, month and year and calculate how many days in the year have passed. For example, if the user enters 3rd February 2007, then that is the 34th day of the year. (31 days in January, plus 3 in February). The program should store the number of days in each month in an appropriate data structure. The number of days are as follows:

 January 31 days
 February 28 days
 March 31 days
 April 30 days
 May 31 days
 June 30 days
 July 31 days
 August 31 days
 September 30 days
 October 31 days
 November 30 days
 December 31 days

Note that leap years occur every four years, and during a leap year the month of February has 29 days instead of 28. The program should determine whether the year is a leap year or not. In the case of a leap year, an additional day must be added to the calculation if the input month is later than February. The program should make use of subroutines, and follow standard naming conventions.
Reply
#2
(Jun-13-2019, 09:41 AM)callanlove Wrote: Note that leap years occur every four years, and during a leap year the month of February has 29 days instead of 28.

Despite the fact that this is homework there shouldn't be outright wrong statements. Leap year is not occurring every fourth year.

From wikipedia leap year article you can find correct algorithm for calculating leap year:


Quote:The following pseudocode determines whether a year is a leap year or a common year in the Gregorian calendar (and in the proleptic Gregorian calendar before 1582). The year variable being tested is the integer representing the number of the year in the Gregorian calendar.

if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
(Jun-13-2019, 10:38 AM)perfringo Wrote:
(Jun-13-2019, 09:41 AM)callanlove Wrote: Note that leap years occur every four years, and during a leap year the month of February has 29 days instead of 28.
Despite the fact that this is homework there shouldn't be outright wrong statements. Leap year is not occurring every fourth year. From wikipedia leap year article you can find correct algorithm for calculating leap year:
Quote: The following pseudocode determines whether a year is a leap year or a common year in the Gregorian calendar (and in the proleptic Gregorian calendar before 1582). The year variable being tested is the integer representing the number of the year in the Gregorian calendar. if (year is not divisible by 4) then (it is a common year) else if (year is not divisible by 100) then (it is a leap year) else if (year is not divisible by 400) then (it is a common year) else (it is a leap year)

This isnt homework, this is just a task i was given and i needed help with the code...
Reply
#4
If it's not homework then you shouldn't post it under 'Homework'

If it's not homework then you should calculate leap years correctly.

If you need help with code you should show some code.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
Hint: I believe using modulus could be helpful in this problem.
Reply
#6
(Jun-13-2019, 10:51 AM)perfringo Wrote: If it's not homework then you shouldn't post it under 'Homework'
Thread moved to Homework section by me
Reason - it looks like homework,sounds kike homework and no effort was made by OP
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
Break it into a series of tasks, then code for each task. For example
Store the months' number of days in an appropriate structure
Get the date
Correct February for leap year (reasonable candidate for a subroutine)
Add days in each month up to but not including the selected month
Add the days for the entered date
Output result

Get that started, see where you get stuck, and post your work.
Reply
#8
There are lot of accusations that programming sites are very hostile places. To prove that this is not the case and there are actually friendly and fun-loving people I provide following solution (with caution).

Caution: there are times when one needs 'blue-pill' which will make all problems go away. One should be aware of the toll such a pill takes. Following is for situation: 'this-is-not-homework-but-I-am-not-allowed-to-use-built-in-tools-and-there-are-some-mandatory-expressions-I-have-to-use-but-it-is-still-not-homework'. Use responsibly and avoid overdose.

Following monty-liner delivers number of days from start of year with taking account actual leap years (not imaginary ones which happen on every fourth year):

>>> date = '13.06.2019'
>>> day, month, year = (int(x) for x in date.split('.'))
>>> sum([[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i != 2 else [28, 29][year % 4 == 0 and year % 100 != 0 or year % 400 == 0] for i in range(1 ,13)][:month-1] + [day])
164 
As one can see all the 'work' is done on one line using list comprehension, conditional expressions, some built-in functions and operators, indexing and slicing. No fancy calendar module etc. No need for data structures or subroutines (and yes, this is actual working Python code which delivers expected result)
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
Agree on the friendliness and love the code!
Note that the non-homework assignment though requires use of a data structure for the months and the use of subroutines. And the generic blue pill is white. Don't ask ;^)
Reply
#10
(Jun-13-2019, 03:13 PM)jefsummers Wrote: Note that the non-homework assignment though requires use of a data structure for the months and the use of subroutines.

Guilty as charged (almost). Smile There is no subroutine but I believe that list of days in month qualifies as 'data structure'. Yes, it doesn't have name and will cease to exist but definitely list comprehension creates list. Naming it would have taken fun out of it. And of course - one can define function and return whole thing - this way there will be subroutine as well

# datastructure which holds days of months :-)
[[31, 30][i <= 6 and i % 2 == 0 or i > 7 and i % 2 != 0] if i != 2 else [28, 29][year % 4 == 0 and year % 100 != 0 or year % 400 == 0] for i in range(1 ,13)]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020