While loops in Procedures

296 views Asked by At

I am trying to output 1 to 30 days but it isn't working and it says Your code didn't display any output

here is my code:

def nextDay(year, month, day):  
        day = 0   
        while (day < 30):  
           day = day + 1
           print day  

this what they are having me do. But i am stuck on the day portion. Sorry i noticed I put month instead of day so i fixed it, but this is what I am trying to get to at the end.

Define a simple nextDay procedure, that assumes every month has 30 days.

For example:

nextDay(1999, 12, 30) => (2000, 1, 1)  
nextDay(2013, 1, 30) => (2013, 2, 1)  
nextDay(2012, 12, 30) => (2013, 1, 1)    (even though December really has 31 days)


def nextDay(year, month, day):
"""   
Returns the year, month, day of the next day.  
Simple version: assume every month has 30 days.  
"""
# YOUR CODE HERE  
return
3

There are 3 answers

0
itzMEonTV On

Did you want this

 def nextDay(year, month, day):
     if day == 30:
         if month == 12:
             day, month = 1, 1
             year+=1
         else:
             month+=1
     else:
         day+=1
     return (year, month, day)

>>>nextDay(2012, 12, 30)
(2013, 1, 1)
0
Moon Cheesez On

I hope this is what you needed.

def nextDay(year, month, day):
    day += 1
    if day > 30:
        day = 1
        month += 1
        if month > 12:
            month = 1
            year += 1
    return (year, month, day)

Your code did not show anything as I don't think you have called the function.

3
Sakamaki Izayoi On

Well if you're trying to output 1 through 30 this will work...

for x in range(1, 31):
    print 'Day: %d' % x

I literally don't get your function at all, as it makes no sense.

In addition, I don't really get why you would use a while loop for that as it is slower than both range and xrange.