2013-10-31 13 views
-1

我正在編寫一個程序,計算一個人在一段時間內的收入金額,如果他的工資是第一天的一分錢,第二天便是兩便士,並繼續每天兩次。 我把一切都做,但我必須進入美元,我不知道如何做到這一點只漂浮給了我0.0當我需要.00python便士一天數字功能

感謝

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

回答

3

試試這個:

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

This documentation更詳細地描述了字符串格式。

如果您正在使用Python版本是爲這個太舊(< 2.7,我認爲),嘗試:

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

如果您正在使用Python版本是太老了(< 2.6 ,我認爲),嘗試:

print "%.2f"%el 
+0

它沒有工作我得到了一個錯誤 –

+2

你使用的是什麼版本的Python?你得到了什麼錯誤?你是如何解決這個錯誤的(或者你是如何試圖解決的)? –

3

另外,您可以使用currency formatting

>>> 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' 

(禮貌美國洛特)

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

-1

Redid這個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"); 

      } 
     } 
    } 
}