| | |
Help: rational number to decimal with certain digits
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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.)
Any suggestions would be greatly appreciated. And I'm sorry to add another lame homework question but I am stumped.
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)
def rationalnum(n, d, num_iterations): A = n/d B = n%d print A print '.' for n in range(num_iterations): n = B*10 c=n/d n=n%d print c def main(): num = input('Please Enter a numerator to the rational number: ') denom = input('Please Enter a denominator to the rational number: ') num_iterations = input('Please Enter a number of decimal places to show: ') rationalnum(num, denom, num_iterations) main()
Any suggestions would be greatly appreciated. And I'm sorry to add another lame homework question but I am stumped.
•
•
Join Date: Dec 2006
Posts: 1,031
Reputation:
Solved Threads: 290
Your problem is here 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. 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".
Python Syntax (Toggle Plain Text)
for n in range(num_iterations): print "n before =", n n = B*10 print "n after =", n
Python Syntax (Toggle Plain Text)
def rationalnum(n, d, num_iterations): whole, remain = divmod(n, d) print "divmod =", whole, remain print "% =", n%d, n, d ## convert to decimal dec = float(remain) / d print "dec =", dec ##--- you could also use this to convert number = float(n) / d number_int = int(number) number_decimal = number - number_int for n in range(num_iterations): dec *= 10 print dec print "\nthe decimal multiplied %d times = %d\n" % \ (num_iterations, int(dec)) def main(): num = input('Please Enter a numerator to the rational number: ') denom = input('Please Enter a denominator to the rational number: ') num_iterations = input('Please Enter a number of decimal places to show: ') rationalnum(num, denom, num_iterations) main()
Last edited by woooee; Oct 1st, 2009 at 1:37 am.
Linux counter #99383
We will give you homework help, as long as you show an effort! However, we will not do the homework for you.
May 'the Google' be with you!
![]() |
Similar Threads
- Karatsuba Wrong Answer After 6 Decimal digits (C++)
- How to configure GD (PHP)
- php.ini confusion (PHP)
- string to float (C)
- How to set decimal digits to 2 for a double type data field in grid control (Visual Basic 4 / 5 / 6)
- the value of pie (C++)
- Numbers (C++)
- Setprecision help please (C++)
- setting the number of decimal points in double varibale (C)
Other Threads in the Python Forum
- Previous Thread: How to create a real-time numeric display in python?
- Next Thread: debug with pdb
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change chmod converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






