June 10th, 2013, 08:01 PM

[2.75] First time learning python, homework question!
Homework question :
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.
Sample run #1:
>>>
18 years and 27 months are equivalent to: 243 months
243 total months are equivalent to: 20 years and 3 months
I've just started learning python so i'm really lost...
Do I start like
x= year
y= months x= 5
y= 9 print x*12
print y/12 + 11
this code is terribly, horribly, wrong, but this is my first time trying to learn python >< any help would be SO appreciated. thank you.
June 10th, 2013, 08:53 PM

First assign and calculate your values. Then print.
Code:
#first asign some variables
years = 72
months = 57
months_in_a_year = 12
total_months = your_equation_here
inverse_result = your_equation_here
#now print stuff.
Mek
Comments on this post
June 10th, 2013, 09:00 PM

Are you able to run the python interpreter? If so, read this while trying experiments in the tutorial.
http://docs.python.org/2/tutorial/introduction.html
If you cannot start the interpreter, start here
I'd solve the hard question like this:
Code:
proper_months = 243
years_and_months = divmod(proper_months, 12,)
print('{} total months are equivalent to: {} years and {} months'
.format(proper_months, *years_and_months))
Oh no that was way too complicated.
Code:
proper_months = 243
years = proper_months // 12
remainder = proper_months  years * 12
print('{} total months are equivalent to: {} years and {} months.'
.format(proper_months, years, remainder))
If you understand the // operator you can also use % . Your instructor might expect an answer like this:
Code:
proper_months = 243
years = proper_months // 12
months = proper_months % 12
print('{} total months are equivalent to: {} years and {} months.'
.format(proper_months, years, months))
Now try to define a variable, and to print it.
Or I might go overboardfor the C or D grade
Code:
def antibase(x, y):
'''
doctest. Run with bash command $ python m doctest this_file.py
Find base 10 digits of small numbers.
>>> antibase([10]*8, 23456)
[0, 0, 0, 2, 3, 4, 5, 6]
>>>
convert $1.38 to coins.
A quarter is 2.5 times more valuable than a dime.
A dime has twice the value of a nickel.
A nickel is worth 5 pennies.
A penny is worth 1.
The output [5.0, 1.0, 0.0, 3.0, 0.0] represents
the number of quarters, dimes, nickels, pennies, and fractional pennies.
>>> US_COINS = antibase([9999, 2.5, 2, 5, 1], 138)
>>> print(US_COINS)
[5.0, 1.0, 0.0, 3.0, 0.0]
>>>
'''
n = y
base = x
cumprod = [1]
for b in reversed(base):
cumprod[:0] = [cumprod[0]*b]
result = []
for f in cumprod[:1]:
(a,n,) = divmod(n,f,)
result.append(a)
result.append(n)
return result[1:]
Now
>>> antibase([9999,12],243)
[20, 3]
Comments on this post
[code]
Code tags[/code] are essential for python code and Makefiles!
June 16th, 2013, 09:53 PM

thanks, this really helped!