2017-06-20 22 views
0

我剛剛進入Python,我是一個新手說至少。我在PyCharm玩耍,我正在嘗試爲員工數據輸入添加一些基本信息。轉換爲貨幣和打印雙週計劃

我相信你可以看到我的腳本中的要點。無論如何,首先我已經嘗試了一些想法,讓它能夠將薪水,獎金和年度作爲貨幣吐出,但不能。你知道60000美元,而不是60000.

此外,如果你在第一次支付工資的日期輸入,我希望它給出一個雙週薪酬計劃。

我正在嘗試腦力風暴。

name = input('Enter Employee Name: ') 
# This should be an integer that represents the age of an employee at GPC 
try: 
    age = int(input('Enter Employee Age: ')) 
except: 
    print('Please enter a whole number') 
    exit() 
job = input('Enter Employee Job: ') 
# This should be an integer that represents the salary of the employee at 
GPC 
try: 
    salary = int(input('Enter Employee Salary to the Nearest Dollar: ')) 
except: 
    print('Please enter only numbers without foreign characters') 
    exit() 
salary_bonus = int(input('Enter employee bonus percentage '))/100 
annual_income = (salary * salary_bonus) + salary 
import datetime 
now = datetime.datetime.now() # Current Year 
u = datetime.datetime.strptime('2016-12-30','%Y-%m-%d') 
d = datetime.timedelta(weeks=2) 
t = u + d 

# Data Output 

print('Employee: ', name) 
print('Age: ', age) 
print('Job Title: ', job) 
print('Annual Salary: ', round(salary,2)) 
print('Biweekly Paycheck: ', round(salary/26, 2)) 
print('Bonus for', now.year, ': ', round(salary * salary_bonus, 2)) 
print('Actual Annual Income: ', round(annual_income, 2)) 

print('Your pay schedule will be:') 
print(t) 
print(t+d) 
+0

[轉換浮動,以美元和美分(可能的重複https://stackoverflow.com/questions/21208376/converting -float-to-dollars-and-cents) – snb

+0

我試過了,並且無法讓它正常工作。所以問我的腳本指導,因爲我不明白爲什麼它不起作用。 –

+0

你是什麼意思,你不能得到它的工作?它的一行......''$ {:,。2f}'。格式(1234.5)'。只需用你的號碼代替1234.5,就是這樣。它的格式字符串,基本上任何帶{}的字符串都可以用作格式。如果您需要進一步解釋* why *的作用,請轉到我提供的鏈接。 – snb

回答

0

您可以使用local.currency

import locale 
locale.setlocale(locale.LC_ALL, '') 
print('Actual Annual Income: ', locale.currency(round(annual_income, 2), grouping=True)) 

locale.currency(VAL,符號=真,分組=假,國際= FALSE)

格式化根據當前一些VAL LC_MONETARY設置。

如果symbol爲true,則返回的字符串包含貨幣符號,這是默認值。如果分組爲真(這不是默認值),則使用該值完成分組。如果國際是真的(這不是默認),則使用國際貨幣符號。

請注意,此函數不適用於'C'語言環境,因此您必須先通過setlocale()設置語言環境。

編號:Python Documents: Internationalization services

PS:對於雙週薪時間表,請參閱Generating recurring dates using python?

+0

這工作!謝謝! –