A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • one year ago

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 .

  • This Question is Closed
  1. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Please post your code so we can check it out.

  2. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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))

  3. anonymous
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

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

  4. rsmith6559
    • one year ago
    Best Response
    You've already chosen the best response.
    Medals 0

    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.

  5. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.