0

Maybe I'm not seeing what the problem is because I've been thinking of the question all day long and I've already gotten help (but thought I could get the other part of this myself.) I'm trying to get

Day      Salary     Total Pay
______________________________
1         .01         .01
2         .02         .03
3         .04         .07
4         .08         .15
5         .16         .31
etc

but I'm having a hard time with the total pay part of the math.

for days in range(1, days + 1):
      for days in range(1, days + 1):
          for days in range(1, days +1):
              salary = .01*2**(days-1)
              total_pay = salary*.01*2**(days-1)    

This is what I have so far, but I can't for the life of me get the math to sort out. I can get close with what I have, but not what I need. Everything else works, but that total pay. would greatly appreciate any help with such a simple problem.

4

2 回答 2

0

一个简单的递归解决方案:

>>> def total_pay(day):
...     if day == 1:
...         return 0.01
...     return 0.01*2**(day-1)+total_pay(day-1)

>>> total_pay(2)
0.03
>>> total_pay(3)
0.07
>>> print("{0:.2f}".format(total_pay(5)))
0.31
于 2012-10-27T03:15:06.203 回答
0

total pay是前total_pay一天工资加上当天工资的总和。

total_pay = 0
for days in range(1, days + 1):
    salary = .01*2**(days-1)
    total_pay += salary
于 2012-10-27T02:35:10.797 回答