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 .
Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
Please post your code so we can check it out.
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.
f = fh + i/12
I'm a code beginner, so I don't quite understand much yet. Sorry.
Not the answer you are looking for? Search for more explanations.
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.