0

I have designed a code which will take a 'number' as an input from the user.

The number will be used to make a...

  1. numerator = (3*number) - 2
  2. and a denominator, which will be denominator = (4*n) + 1.

The code will also allow the user to choose how many times they want this sequence to go on after which the sum of all the fractions will be totaled and displayed.

Here is the Code I have:

l=int(input("How many times do you repeat this sequence?: "))
n=int(input("Enter a base number: "))
n1=n
n2=n
total=0
s = ''
def calculate(l,n,n1,n2,total,s):
    for j in range(l):
        s += "{}/{} + ".format(3*n1-2, 4*n2+1)
        n1=n+n1
        n2=n+n2
        total=(((n*3)-2)/((4*n)+1))+total
    print(s)
    print(total)

calculate(l, n, n1, n2, total, s)

Now here are the two errors that I receive when I get the output for this code for example:

How many times do you repeat this sequence?: 2
Enter a base number: 1
1/5 + 4/9 + 
0.4

The two Issues:

  1. Since 4/9 is the last fraction, is there a way to get rid of that "+" addition sign at the end, because it just points to a blank space..
  2. The total for the two fractions shows to be 0.4 which is incorrect, the total sum should be 1/5 + 4/9 = 0.2 + 0.44 = 0.64, I am unsure where I went astray when inputting my total sum formula above.

Any suggestions/comments would be appreciated!

1
  • I suggest using more meaningful variable names so that people can follow your code a little easier. Commented Mar 8, 2015 at 19:15

3 Answers 3

1

A cheap way of removing the + would be to simply cut off the last character in the string: str[:-1].

As far a issue 2 goes, it looks like you want to use n1 and n2 instead of n.

As of now, you're getting 1/5(.2) + 1/5(.2) = .4

Sign up to request clarification or add additional context in comments.

1 Comment

I tried something like this as well: n1=n+n1 n2=n+n2 total=(n1/n2)+total and got: How many times do you repeat this sequence?: 2 Enter a base number: 1 1/5 + 4/9 total=2.0
0
  1. Instead of concatening a string like that, collect all the parts in a list and then join the items on the plus sign:

    s = []
    s.append('{}/{}'.format(1, 5))
    s.append('{}/{}'.format(4, 9))
    print(' + '.join(s)) # 1/5 + 4/9
    
  2. I’m not really sure what you are doing but if you want to get the sum of the fractions you print, you should just make sure that you calculate those individual fractions in the same way. So instead of incrementing n1 and n2 first before calculating the sum, calculate the sum in the same way you did for the fraction output and only afterwards change those variables:

    s.append("{}/{}".format(3 * n1 - 2, 4 * n2 + 1))
    total += (3 * n1 - 2) / (4 * n2 + 1)
    n1 += n
    n2 += n
    

1 Comment

I tried the code for your second point and it gave me this output :1/5 + 4/9 total =0.9829059829059829, which is is now greater than 0.64
0

I dont know python but you could do the following to correct your logical errors.

  1. to remove the '+' at the end, you can do something like below,

if j = l (implies last fraction) dont include + else include +

  1. While calculating total you are using 'n' value which always remains as your input value total=(((n*3)-2)/((4*n)+1))+total Here use n1 or n2 total=(((n1*3)-2)/((4*n2)+1))+total

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.