Question:
Python Coding Problem?
2013-06-09 12:18:05 UTC
Write a Python program that initializes (assigns initial values to) two variables (provide adequate
naming of variables!) one consisting of a (positive, integer) number of years, and the other a
(positive, integer) number of months (not necessarily less than 12). Assign some values directly
to these variables, no input is required from the user.
Then the program should calculate a single value in months (equivalent to the total years and
months) and then print all the values (original and calculated), with an adequate message (see
below).
How would you do the inverse process? From a total number of months calculate the number of
years and months (with 11 as the maximum number of months in the final result)?
So that you can better imagine what this program should do, see an example of the execution of
this program (a sample run) next.

First time ever programming, don't really understand the problem. If I assigned a random variable to months and years, how would I continue with the program?
Three answers:
Bob
2013-06-09 12:30:50 UTC
import random



months = random.randint(1, 12)



years = random.randint(0, 5)



totalNumberOfMonths = months + (12 * years)



print '%d years and %d months are equivalent to: %d months' % (years, months, totalNumberOfMonths)
Michael
2013-06-09 19:33:02 UTC
So this is a basic math problem. Lets say I assign my variables like so:



Years = 12

Months = 4



to calculate the total number of months, I do some simple arithmetic. Remember, there are 12 months in a year. So if we multiply the number of years by 12 we can get the number of months in x amount of years. We can then take this product, and add our months variable to it, and then we would have the total amount of months. In code, that would look like:



TotalMonths = (Years * 12) + Months



Then you would just print out the answer with the other information.



Now, I dont want to just give you the answers, since you cant learn that way. For the second part of the question, we do something similar to the above, but instead of multiplying number of years by 12 to get number of months, we would divide number of months by 12 and that would give us number of years.



Hope this helps
2013-06-09 19:50:02 UTC
initialize 2 variables like this

# it is similar to say year=18, month=27



year, month = 18, 27



# now calculate total months of 18 years and add 27 months



total_months = year*12 + month



#now print a message



print year, "years and", month ,"months are equivalent to:", total_months ,"months"



#calculate how many total years you can make out of total_months

total_year = month/12



#and find out how much months still left after making whole years (confusing)

# for example 14 months can make 1 whole year but still 2 months are left that you have to find now

# % (remainder) is used to do that



months_left = month%12



# now print a last message

print total_months, "total months are equivalent to:", total_year, "years and" ,months_left, "months"



#all the best ;)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...