2016-07-19 53 views
0

私はpython 3で学校の給与計算機を書いています。ユーザー入力はあなたの名前を要求するか、プログラムを終了することから始まります。開始時に "0"を入力するとプログラムは終了するはずですが、ユーザーが計算した後に入力すれば、印刷されます(レポートの終わりと以前の給与情報)。私はそれを終了した後に給与情報の印刷を停止する方法を理解できません。これは私がこれまで持っていたものです。給与計算機Python

これはコードです: ワン・ストップ・ショップ給与計算

user = str 
end = "0" 
hours = round(40,2) 
print("One Stop Shop Payroll Calculator") 
while user != end: 
    print() 
    user = input("Please enter your name or type '0' to quit: ") 
if user == end: 
    print("End of Report") 
else: 
    hours = (float(input("Please enter hours worked: ",))) 
    payrate =(float(input("Please enter your payrate: $",))) 
if hours < 40: 
    print("Employee's name: ", user) 
    print("Overtime hours: 0") 
    print("Overtime Pay: $0.00") 
    regularpay = round(hours * payrate, 2) 
    print("Gross Pay: $", regularpay) 
elif hours > 40: 
    overtimehours = round(hours - 40.00,2) 
    print("Overtime hours: ", overtimehours) 
    print("Employee's name: ", user) 
    regularpay = round(hours * payrate,2) 
    overtimerate = round(payrate * 1.5, 2) 
    overtimepay = round(overtimehours * overtimerate) 
    grosspay = round(regularpay+overtimepay,2) 
    print("Regular Pay: $", regularpay) 
    print("Overtime Pay: $",overtimepay) 
    print("Gross Pay: $", grosspay) 

これは、あなたがそれを実行したときにそれが現れる方法です:

One Stop Shop Payroll Calculator 

Please enter your name or type '0' to quit: Brandon 
Please enter hours worked: 50 
Please enter your payrate: $10 
Overtime hours: 10.0 
Employee's name: Brandon 
Regular Pay: $ 500.0 
Overtime Pay: $ 150 
Gross Pay: $ 650.0 

Please enter your name or type '0' to quit: Brandon 
Please enter hours worked: 30 
Please enter your payrate: $10 
Employee's name: Brandon 
Overtime hours: 0 
Overtime Pay: $0.00 
Gross Pay: $ 300.0 

Please enter your name or type '0' to quit: 0 
End of Report 
Employee's name: 0 
Overtime hours: 0 
Overtime Pay: $0.00 
Gross Pay: $ 300.0 

Process finished with exit code 0 

code

code being executed

+0

文字列を '=='で比較するのを止めます。 – csmckelvey

答えて

0

だから私はそれを持っていると思う。私はあなたのコードのほとんどをコピーして、それはすべてうまくいきます。私の変更点を説明して、なぜこれが効果があるのか​​を知るのに役立ちます。

最初に、いつ終了するかを定義する方法を変更しました。 end = "False" while whileループは実行されます。end = "0"ではなくend = Falseに設定されます。それが言うよう

は、それからわずかにwhileループの最初の行を変更:もしユーザ==「0」:

これは入力「ユーザ」をチェックし、その= 0の場合は、次の行を実行します。

私は印刷物を残しましたが、休憩を追加しました。それはここの鍵です。中断がなければ、「終了報告」の後でループを継続するだけです。しかし、休憩をとると、「終了報告」を印刷した後にループから抜け出します。

私も行を変更する必要がありました:

:それ以外の場合は40を入力として、「時間< = 40があれば、」

を動作しないでしょうに「時間> 40の場合を」だから、今のコードは次のようになります

user = str 
end = False 
hours = round(40,2) 
print("One Stop Shop Payroll Calculator") 
while end == False: 
    user = input("\nPlease enter your name or type '0' to quit: ") 
    if user == "0": 
    print("End of Report") 
    break 
    else: 
    hours = (float(input("Please enter hours worked: ",))) 
    payrate =(float(input("Please enter your payrate: $",))) 
    if hours <= 40: 
    print("Employee's name: ", user) 
    print("Overtime hours: 0") 
    print("Overtime Pay: $0.00") 
    regularpay = round(hours * payrate, 2) 
    print("Gross Pay: $", regularpay) 
    elif hours > 40: 
    overtimehours = round(hours - 40.00,2) 
    print("Overtime hours: ", overtimehours) 
    print("Employee's name: ", user) 
    regularpay = round(hours * payrate,2) 
    overtimerate = round(payrate * 1.5, 2) 
    overtimepay = round(overtimehours * overtimerate) 
    grosspay = round(regularpay+overtimepay,2) 
    print("Regular Pay: $", regularpay) 
    print("Overtime Pay: $",overtimepay) 
    print("Gross Pay: $", grosspay) 

神様私は、空白の字下げでコードをコピーしたいと思います。このハハを編集する必要がありません

+0

悪いことではありません。また、 'end'を取り除き、明示的に' break'でループを終了しているので、 'while True:'を使うこともできます。また、同じ理由でループ内で 'else:'を取り除くこともできます。 'break'が実行された場合、' break'の後のループ内に何も起こりません。 – cxw

+0

ああ、ええ、良い点@cxwは私が少し最適化されていないので、中には、Pythonを使用していない笑 – NicheQuiche

0

私も同様に作った。ここではPython 3xの私です。

print("Welcome to PayCalc!\n") 
wage = float(input("How much do you make per hour?\n")) 
hours = float(input("How many hours for the week?\n")) 
def as_currency(amount): 
    if amount >= 0: 
     return '${:,.2f}'.format(amount) 
    else: 
     return '-${:,.2f}'.format(-amount) 
if hours <= 40: 
    weekincome = wage*hours 
    monthincome = weekincome*4 
    print("It has been calculated that if you work {} hours at a rate of {}, you should make a total of {}/week ({}/month)".format(int(round(hours)),as_currency(wage),as_currency(weekincome),as_currency(monthincome))) 
else: 
    regularpay = wage*40 
    overtimehours = hours - 40 
    overtimerate = wage*1.5 
    overtimeincome = (overtimehours * overtimerate) 
    print("Regular pay: {}/wk + your overtime rate of {}/hr".format(as_currency(regularpay),as_currency(overtimerate))) 
    print("Hours of overtime: {}".format(int(round(overtimehours)))) 
    print("Total overtime income: {}".format(as_currency(overtimeincome))) 
    weekincome = (40*wage) + overtimeincome 
    #if worked overtime every week 
    monthincome = weekincome*4 
    overtimeonce = weekincome + (regularpay*3) 
    print("It has been calculated that you should make a total of {}/week with overtime ({}/month) if worked {} hours every week.\nIf worked {} hours during one week and 40 hours/wk every other week, you'd make {} for the month".format(as_currency(weekincome),as_currency(monthincome),int(round(hours)),int(round(hours)),as_currency(overtimeonce))) 

、40時間以上の40時間^^^、又は、

Welcome to PayCalc! 

How much do you make per hour? 
19.12 
How many hours for the week? 
60 
Regular pay: $764.80/wk + your overtime rate of $28.68/hr 
Hours of overtime: 20 
Total overtime income: $573.60 
It has been calculated that you should make a total of $1,338.40/week with overtime ($5,353.60/month) if worked 60 hours every week. 
If worked 60 hours during one week and 40 hours/wk every other week, you'd make $3,632.80 for the month 

かの下場合には、例えば出力

Welcome to PayCalc! 

How much do you make per hour? 
19.12 
How many hours for the week? 
40 
It has been calculated that if you work 40 hours at a rate of $19.12, you should make a total of $764.80/week ($3,059.20/month) 

あろう。

関連する問題