2016-08-07 45 views
0

Der Versuch, dieses Gehaltsrechner-Skript zu schreiben, aber die Ausgabe berechnet die Steuern zweimal. Es tut, was es tun soll, aber ich missbrauche das Konzept der Rückkehrerklärung, denke ich. Ich bin ein wenig neu bei Python und starte gerade mit dem DSA. Ich habe versucht, für dieses Problem viel zu suchen, aber abgesehen von der Information für die Rücksendeaussage konnte ich dieses wiederkehrende Aussageproblem nicht lösen. Ich hätte gerne Ihre Vorschläge für den Rest des Programms.Wie benutzt man die Return-Funktion, vermeidet aber die Wiederholung einer Funktion?

Danke!

Hier ist mein Code:

import math 

# Personal Details 
name = input("Enter your first name: ") 
position= input("Enter your job position: ") 

def regPay(): 
#Computes regular hours weekly pay 
    hwage= float(input("Enter hourly wage rate: ")) 
    tothours=int(input("Enter total regular hours you worked this week: ")) 
    regular_pay= float(hwage * tothours) 
    print ("Your regular pay for this week is: ", regular_pay) 
    return hwage, regular_pay 

def overTime(hwage, regular_pay): 
#Computes overtime pay and adds the regular to give a total 
    totot_hours= int(input("Enter total overtime hours this week: ")) 
    ot_rate = float(1.5 * (hwage)) 
    otpay= totot_hours * ot_rate 
    print("The total overtime pay this week is: " ,otpay) 
    sum = otpay + regular_pay 
    print("So total pay due this week is: ", sum) 
    super_pay = float((9.5/100)*sum) 
    print ("Your super contribution this week is:",super_pay) 
    return super_pay 

def taxpay(): 
#Computes the taxes for different income thresholds, for resident Aussies. 
    x = float(input("Enter the amount of your yearly income: ")) 

    while True: 
     total = 0 
     if x < 18200: 
      print("Congrats! You dont have to pay any tax! :)") 

      break 
     elif 18201 < x < 37000: 
      total = ((x-18200)*0.19) 
      print ("You have to pay AUD" ,total , "in taxes this year") 
      return x 


      break 
     elif 37001 < x < 80000: 
      total = 3572 + ((x-37000)*0.32) 
      print("You have to pay AUD",(((x-37000)*0.32) +3572),"in taxes this year") 
      return x 

      break 
     elif 80001 < x < 180000: 
      total = 17547+((x-80000)*0.37) 
      print ("You have to pay AUD" ,total ,"in taxes this year") 
      return x 
      break 
     elif 180001 < x: 
      total = 54547+((x-180000)*0.45) 
      print ("You have to pay AUD" , total ,"in taxes this year") 
      return x 
      break 
     else: 
      print ("Invalid input. Enter again please.") 
      break 


def super(x): 
#Computes super over a gross income at a rate of 9.5% 
    super_rate = float(9.5/100) 
    super_gross = float((super_rate)*(x)) 
    print ("Your super contribution this year is: ",super_gross) 




def main(): 
#Main function to pass vars from regPay to overTime and call. 
    hw , r_p = regPay() 
    overTime(hw, r_p) 
    taxpay() 
    y = taxpay() 
    super(y) 

#Call main 
main() 

Der Ausgang I in Powershell erhalten:

PS C:\Users\tejas\Desktop\DSA> python salary_calc.py 
Enter your first name: tj 
Enter your job position: it 
Enter hourly wage rate: 23 
Enter total regular hours you worked this week: 20 
Your regular pay for this week is: 460.0 
Enter total overtime hours this week: 20 
The total overtime pay this week is: 690.0 
So total pay due this week is: 1150.0 
Your super contribution this week is: 109.25 
Enter the amount of your yearly income: 20000 
You have to pay AUD 342.0 in taxes this year 
Enter the amount of your yearly income: 20000 
You have to pay AUD 342.0 in taxes this year 
Your super contribution this year is: 1900.0 
+1

Welche Ausgabe haben Sie stattdessen erwartet? –

+0

Ich denke, er meint diesen Teil: 'Geben Sie die Höhe Ihres Jahreseinkommens: 20000 Sie müssen AUD 342.0 in Steuern in diesem Jahr zahlen Geben Sie die Höhe Ihres Jahreseinkommens: 20000 Sie müssen in diesem Jahr AUD 342,0 Steuern zahlen ', die die Eingabeaufforderung für die Steuern genau wiederholt –

+0

. Ja. –

Antwort

1

Von Ihrem Ausgang, ich sehe es zweimal für das Jahreseinkommen fragt:

Enter the amount of your yearly income: 20000 
You have to pay AUD 342.0 in taxes this year 
Enter the amount of your yearly income: 20000 
You have to pay AUD 342.0 in taxes this year 

Sieht aus wie Ihr Problem hier ist:

def main(): 
#Main function to pass vars from regPay to overTime and call. 
    hw , r_p = regPay() 
    overTime(hw, r_p) 
    taxpay() 
    y = taxpay() 
    super(y) 

Sie rufen taxpay(), dann y auf einen anderen Anruf von taxpay(). Meinst du das nur einmal anzurufen? Wenn ja, versuchen Sie stattdessen:

def main(): 
#Main function to pass vars from regPay to overTime and call. 
    hw , r_p = regPay() 
    overTime(hw, r_p) 
    y = taxpay() 
    super(y) 
+0

Okay. Sieht so aus, als ob ich die Steuerfunktion zweimal indirekt angerufen hätte. Ta! –

0

Einfach gesagt, die beiden folgenden Aufruf/execute die taxpay Funktion, so wird dies wiederholen genau die gleiche Leistung, wenn Sie globale Variablen ändern, die nicht, wie Sie sieht sind.

Nur die zweite Zeile gibt den Wert in die Variable y zurück.

taxpay() 
y = taxpay() 

Zusammen mit diesem Punkt hier rufen Sie die Funktion, aber nie erfassen seine zurückgegebenen Ausgabe

overTime(hw, r_p) 

Und so nebenbei, Sie wollen nicht auf die Eingangsschleife auf ungültige Eingabe brechen .

print ("Invalid input. Enter again please.") 
break # should be 'continue' 
+0

Danke für die Rückmeldung. Bekannt. –