-1

我正在编写一个程序,该程序将计算一个人在一段时间内的收入,如果他或她的薪水第一天是一便士,第二天是两便士,并且每天继续翻倍。我已经完成了所有工作,但我必须投入美元,我不知道该怎么做,浮动只会在我需要 .00 时给我 .0

谢谢

# Ryan Beardall Lab 8-1 10/31/13
#program calculates the amount of money a person would earn over a period of time if his or her salary is one penny the first day, two pennies the second day and continues to double each day. 
accumulatedPay = []
payPerDay = []
days = []
#User enters days worked to find out ho much money user will have
def numberDays():
    global daysWorked
    daysWorked = (input("Enter the days worked "))
    return daysWorked
def salaryOnDay(daysWorked):
    return float((2**(daysWorked-1))/100)
def calculations():
    earnings = 0
    for i in range (1, daysWorked + 1):
        salary = salaryOnDay(i)       
        currRow = [0, 0, 0]
        currRow[0] = i
        currRow[1] = salary
        earnings += salary
        currRow[2] = earnings
        days.append(currRow)
#program prints matrix
def main():
    numberDays()
    calculations()   
    for el in days:
        print el
main()
4

5 回答 5

3

或者,您可以使用货币格式

>>> import locale
>>> locale.setlocale( locale.LC_ALL, '' )
'English_United States.1252'
>>> locale.currency( 188518982.18 )
'$188518982.18'
>>> locale.currency( 188518982.18, grouping=True )
'$188,518,982.18'

(由 S.Lott 提供)

于 2013-10-31T13:40:34.103 回答
3

尝试这个:

for el in days:
    print '{:.2f}'.format(el)

本文档更详细地描述了字符串格式。

如果您使用的 Python 版本太旧(我认为<2.7),请尝试:

print '{0:.2f}'.format(el)

如果您使用的 Python 版本太旧(我认为<2.6),请尝试:

print "%.2f"%el
于 2013-10-31T13:37:55.283 回答
1

我正在做同样的问题,只是通过将浮点格式定义为 0.01 开始更简单一点:

def main():
    num_input = int(input('How many days do you have? '))
    # Accumulate the total
    total = 0.01
    for day_num in range(num_input):
        if day_num == 0:
            print("Day: ", day_num, end=' ')
            total = total
            print("the pennies today are:", total)
            day_num += day_num
        else:    
            print("Day: ", day_num + 1, end=' ')
            total += 2 * total
            print("the pennies today are:", total)         
main()

输出:你有多少天?5
天:0 今天便士是:0.01
天:2 今天便士是:0.03
天:3 今天便士是:0.09
天:4 便士今天是:0.27
天:5 今天便士是:0.81

于 2016-10-06T00:12:44.133 回答
1

我是这样做的

P = 0.01
ttlP = P
dys = int(input('How many days would you like to calculate?'))
for counter in range(1, dys + 1):
  print('Day',counter,'\t \t \t $',P)
  P = P * 2
  ttlP = ttlP + P
ttlP = ttlP - P
print('Total pay is $',format((ttlP),'.2f'),sep='')
于 2018-06-19T15:32:50.030 回答
-1

为 C# 重做此示例。只需使用 Visual Studio 中的默认控制台应用程序设置即可开始。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HW_1_Penny_Doubling
{
    class Program
    {
        static void Main(string[] args)
        {
            double total = 0.01;
            Console.WriteLine("How many completed days will you be working?");
            int days = int.Parse(Console.ReadLine());

            if(days <= 200)
            {
                for(int i = 1; i <= days; i++)
                {
                    if (days == 0)
                    {
                        Console.WriteLine("At zero days, you won't even make a penny");
                    }
                    else
                    {
                        Console.WriteLine("Day: " + i);
                        total = total + (2 * total); // Take the total and add double that to the original amount
                        Console.WriteLine("Today's total is: " + total);
                    }
                }

            }
            else
            {
                Console.WriteLine("The value will be too high, Try a number below 200");

            }
        }
    }
}
于 2017-01-25T18:28:48.053 回答