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

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.

Your problem is here

for n in range(num_iterations):
        print "n before =", n
        n = B*10
        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.

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

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".

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.

Thank you very much for the help. It definitely was more than helpful :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.