Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
June 10th, 2013, 09:01 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation Power: 0
[2.75] First time learning python, homework question!

Homework question :

Quote:
 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.

#2
June 10th, 2013, 09:53 PM
 Mekire
Contributing User

Join Date: Oct 2012
Posts: 171
Time spent in forums: 3 Days 8 h 10 m 42 sec
Reputation Power: 2
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
lunapt agrees: great! sorry i dont have reps ;;

#3
June 10th, 2013, 10:00 PM
 b49P23TIvg
Contributing User

Join Date: Aug 2011
Posts: 4,142
Time spent in forums: 1 Month 3 Weeks 2 Days 7 h 23 m 47 sec
Reputation Power: 455
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 overboard---for 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]
Mekire agrees: Yeah divmod is definately the right tool. Unfortunately teachers always seem to yell at their
students when they have the audacity to do problems correctly.
__________________
[code]Code tags[/code] are essential for python code!

#4
June 16th, 2013, 10:53 PM
 lunapt
Registered User

Join Date: Jun 2013
Posts: 12
Time spent in forums: 3 h 35 m 26 sec
Reputation Power: 0
thanks, this really helped!

 Viewing: Dev Shed Forums > Programming Languages > Python Programming > [2.75] First time learning python, homework question!