user2405840
user2405840

Reputation: 59

Writing python code to calculate a Geometric progression

Im new to programming and python. I need help with coding a geometric progression thats supposed to calculate the progression 1,2,4,8,16... Heres what I have so far:

def work_calc (days_worked, n):
      temp=int(1)
      if days_worked<2:
            print (1)
      else:
            while temp <= days_worked:
                  pay1 = (temp**2)
                  pay = int(0)
                  pay += pay1
                  temp +=1
                  print ('your pay for ',temp-1,'is', pay1)

main()

Right now it gives me this output: 1, 4, 9, 16, 25 i need : 1,2,4,8,16,32...

im writing code that basically should do this:

Example:
Enter a number: 5
your value 1 is: 1
your value 2 is : 2
your value 3 is : 4
your value 4 is : 8
your value 5 is : 16
your total is: 31

Thanks in advance for your help and guidance! P.S: Im like a dumb blonde sometimes(mostly) when it comes to programming, so thanks for your patience..

Upvotes: 1

Views: 6674

Answers (4)

MWEsser
MWEsser

Reputation: 33

That is all too complicated. Try always to keep things as simple as possible and especially, keep your code readable:

def main():
    i = int(input("how many times should I double the value? "))
    j = int(input("which value do you want to be doubled? "))
    double_value(i,j)

def double_value(times,value):
    for i in range(times):
    i += 1
    value = value + value
    print(f"{i} --- {value:,}")

main()

Hope I could help.

Upvotes: 0

ToxicTeacakes
ToxicTeacakes

Reputation: 1214

Is this a homework question? (insufficient rep to comment)

In the sequence 1,2,4,8,16,32 each term is double the previous term.

So, you can keep a record of the previous term and double it to get the next one.

As others have mentioned, this is the same as as calculating 2^n (not, as I previously stated, n^2) , where n is the number of terms.

print ('your pay for 1 is' 1)
prevpay = 1
while temp <= days_worked:
    pay1 = prevpay*2
    pay = int(0)
    pay += pay1
    temp +=1
    prevpay = pay1
    print ('your pay for ',temp-1,'is', pay1)

Upvotes: 0

AChampion
AChampion

Reputation: 30258

I understand this is probably overkill for what you are looking to do and you've been given great advice in the other answers in how to solve your problem but to introduce some other features of python here are some other approaches: List comprehension:

def work_calc(days):
    powers_of_two = [2**x for x in range(days)]
    for i, n in enumerate(powers_of_two):
        print('your pay for {} is {}'.format(i+1,n))
    print('your total is {}'.format(sum(powers_of_two)))

This is compact and neat but would hold the whole list of 2^n in memory, for small n this is not a problem but for large could be expensive. Generator expressions are very similar to list comprehensions but defer calculation until iterated over.

def work_calc(days):
    powers_of_two = (2**x for x in range(days))
    total = 0
    for i, n in enumerate(powers_of_two):
        total += n 
        print('your pay for {} is {}'.format(i+1,n))
    print('your total is {}'.format(total))

Had to move the total to a rolling calculation and it still calculates 2**n each time, a generator function would avoid power calculation:

import itertools

def powers_of_two():
    n = 1
    while True:
        yield n
        n *= 2

def work_calc(days):
    total = 0
    for i, n in enumerate(itertools.islice(powers_of_two(), days)):
        total += n 
        print('your pay for {} is {}'.format(i+1,n))
    print('your total is {}'.format(total))

As I said overkill, but hopefully introduces some of the other features of python.

Upvotes: 2

roman
roman

Reputation: 117370

As I said, looks like you need powers of 2:

def work_calc (days_worked, n):
    for temp in range(days_worked):
        print ('your pay for ', temp + 1, 'is', 2 ** temp)

if you want to print strings (not tuples as you're doing now):

def work_calc (days_worked):
    for temp in range(days_worked):
        print 'your pay for {} is {}'.format(temp + 1, 2 ** temp)

>>> work_calc(5)
your pay for 1 is 1
your pay for 2 is 2
your pay for 3 is 4
your pay for 4 is 8
your pay for 5 is 16

Just to note - your code is calculating squares of temp, not powers of 2 that's why is not working

Upvotes: 2

Related Questions