Ask your own question, for FREE!
Computer Science 18 Online
OpenStudy (anonymous):

I really need help with my assignment. My code is wrong, I keep getting true, when I was values. Write a function f2fi(f) that takes a non-negative number f as input and returns a pair of numbers (fh,i) such that f = fh + i/12 and fh is an integer i is a non-negative number smaller than 12 .

OpenStudy (rsmith6559):

Please post your code so we can check it out.

OpenStudy (anonymous):

Alright, one sec. I tried re-doing it, but it just got worse. It no longer prints true and it gives me an error. I'm using python. def f2fi(f): fh=i i=i>12 f = fh + i/12 print (str(fh,i))

OpenStudy (anonymous):

I'm a code beginner, so I don't quite understand much yet. Sorry.

OpenStudy (rsmith6559):

We've all been beginners at one time. First thing I notice is the print() function. That's Python v3.x syntax. It doesn't work in Python v2.x. AFAIK, most of the courses in the various OCW sites are Python v2.x. Your use of the i variable has problems. There's a school of thought that functions shouldn't use variables (except for those declared inside the function) that weren't passed into the function. i wasn't passed in, so I have no idea what type of variable it starts off being, which means that I don't know what type fh is after the assignment. Then i is assigned the value of a comparison, which would be a boolean, True or False. Then i is used in division, which if it's True would be a logical 1, or if it's False would be a logical 0, so either 1/12 which is about 0.08 or 0 is added to fh, which we don't know the type of, and assigned to f. str() is designed to convert a variable to a string. So, IMO, your print() should be: print( str( fh ), str( i ) ) Two debugging tricks: 1. Put print statements in to check that the value of a variable is what you expect after each change, and delete the prints after you find the problem. This makes it easier to find where the train is falling off the tracks. 2. Comment the heck out of your code. I've started writing my comments and function headers before I write code and then coming back and filling in the code. The reason for commenting so much during debugging is that while you comment that f = fh... line, you may/will see that what you're expecting it to do isn't what it IS doing. I find this technique is good at finding my logical errors.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!