944,181 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 591
  • Python RSS
Sep 30th, 2009
0

Help: rational number to decimal with certain digits

Expand Post »
Hi, I am fairly new at the whole programming racket and I am having trouble with an assignment for my computer science class. I know homework help is discouraged, I read that sticky, but I'm curious if I am missing a fairly simple mistake.

The assignment is to make a program that takes input for a numerator and a denominator of a rational number and then divides that rational number to a certain amount of decimal places. Basically it does long division and multiplies the remainder by 10 a certain number of times specified by the user. So if the user enters 22, 7, and 20 it should print out 3.14...up to 20 digits of the rational number. My code's behavior is simply 3.1111... though.

Here is my code so far (and I am pretty sketchy with the modulo operator %; so I think I have done something wrong with it.)

Python Syntax (Toggle Plain Text)
  1. def rationalnum(n, d, num_iterations):
  2. A = n/d
  3. B = n%d
  4. print A
  5. print '.'
  6. for n in range(num_iterations):
  7. n = B*10
  8. c=n/d
  9. n=n%d
  10. print c
  11.  
  12. def main():
  13. num = input('Please Enter a numerator to the rational number: ')
  14. denom = input('Please Enter a denominator to the rational number: ')
  15. num_iterations = input('Please Enter a number of decimal places to show: ')
  16. rationalnum(num, denom, num_iterations)
  17.  
  18.  
  19. main()

Any suggestions would be greatly appreciated. And I'm sorry to add another lame homework question but I am stumped.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jaymeaux77 is offline Offline
17 posts
since Sep 2009
Oct 1st, 2009
1

Re: Help: rational number to decimal with certain digits

Your problem is here
Python Syntax (Toggle Plain Text)
  1. for n in range(num_iterations):
  2. print "n before =", n
  3. n = B*10
  4. print "n after =", n
You use "n" twice and are always multiplying the same number, "B". Something like this should give you the idea of how to go about it. Also, I've introduced divmod which is just another way to get the whole integer and the remainder (not decimal). Your main problem is that you haven't tried including print statements to see what is happening, so can not tell that you are getting the remainder as an integer and not as a float/decimal.
Python Syntax (Toggle Plain Text)
  1. def rationalnum(n, d, num_iterations):
  2. whole, remain = divmod(n, d)
  3. print "divmod =", whole, remain
  4. print "% =", n%d, n, d
  5.  
  6. ## convert to decimal
  7. dec = float(remain) / d
  8. print "dec =", dec
  9.  
  10. ##--- you could also use this to convert
  11. number = float(n) / d
  12. number_int = int(number)
  13. number_decimal = number - number_int
  14.  
  15. for n in range(num_iterations):
  16. dec *= 10
  17. print dec
  18.  
  19. print "\nthe decimal multiplied %d times = %d\n" % \
  20. (num_iterations, int(dec))
  21.  
  22. def main():
  23. num = input('Please Enter a numerator to the rational number: ')
  24. denom = input('Please Enter a denominator to the rational number: ')
  25. num_iterations = input('Please Enter a number of decimal places to show: ')
  26. rationalnum(num, denom, num_iterations)
  27.  
  28. main()
Finally, there is no reason to have the main() function, with one orphaned statement that calls it, unless you want to call it from another program, in which case it should be named something more descriptive than "main".
Last edited by woooee; Oct 1st, 2009 at 1:37 am.
Reputation Points: 741
Solved Threads: 694
Nearly a Posting Maven
woooee is offline Offline
2,314 posts
since Dec 2006
Oct 1st, 2009
0

Re: Help: rational number to decimal with certain digits

Click to Expand / Collapse  Quote originally posted by jaymeaux77 ...
Hi, I am fairly new at the whole programming racket and I am having trouble with an assignment for my computer science class. I know homework help is discouraged ...
We will give you homework help, as long as you show an effort! However, we will not do the homework for you.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004
Oct 6th, 2009
0
Re: Help: rational number to decimal with certain digits
Thank you very much for the help. It definitely was more than helpful
Reputation Points: 10
Solved Threads: 1
Newbie Poster
jaymeaux77 is offline Offline
17 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: How to create a real-time numeric display in python?
Next Thread in Python Forum Timeline: debug with pdb





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC