如何使用返回函数但避免重复函数?

时间:2016-08-07 15:37:34

标签: python return python-3.4

尝试编写此工资计算器脚本,但输出会继续计算两次税。它确实做了它应该做的事情,但我认为我滥用了return语句的概念。我是Python的新手,刚开始使用DSA。我尝试了很多这个问题,但除了return语句的信息之外,我无法解决这个反复出现的语句问题。我也希望你对该计划的其余部分提出建议。

谢谢!

这是我的代码:

<html>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Lobster" />
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>

<meta name="viewport" content="width=device-width, initial-scale=1">

<Head>
<title>Puneeth</title>
</Head>

<div class="pageone text-center">
<div class="container-fluid">
<ul class="nav nav-pills">
<li><a href="#">Puneeth S</a></li>
<li class="pull-right"><a href="#">Contact</a></li>
<li class="pull-right"><a href="#">Portfolio</a></li>
<li class="pull-right"><a href="#">About</a></li>
<li class="pull-right"><a href="#">Home</a></li>
</ul>
</div>


<div class="boxone ">
<h1>Puneeth S</h1>
<h3>Engineer by chance, Developer by choice</h3>
</div>
<div class="btn-list text-center">
<a class="btn btn-default" href="#">Resume</a>
<a class="btn btn-default" href="#">Facebook</a>
<a class="btn btn-default" href="#">Twitter</a>
<a class="btn btn-default" href="#">Linkedin</a>
<a class="btn btn-default" href="#">Github</a>
</div>
</div>

<div class="pagetwo">
<div class="row">
<div class="col-md-6 col-lg-6 col-sm-12 col-xs-12">
<img src="https://s20.postimg.org/uguojmpvx/abcd.jpg" alt="trial" class="dp">


</div>
<div class="col-md-6 col-lg-6 col-sm-12 col-xs-12">
<h1 class="text-center">Puneeth S</h1>
<p class="text-center">"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."</p>


</div>
</div>
</div>
</html>

我在PowerShell中获得的输出:

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

2 个答案:

答案 0 :(得分:1)

从你的输出中,我看到它要求年收入两次:

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

看起来你的问题在这里:

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)

您拨打taxpay(),然后将y设置为另一个taxpay()来电。你的意思是只打电话一次吗?如果是这样,请尝试这样做:

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)

答案 1 :(得分:0)

简单地说,以下两个调用/执行taxpay函数,因此这将重复完全相同的输出,除非您修改全局变量,这看起来不像您。

只有第二行会将值返回y变量。

taxpay()
y = taxpay()

除此之外,在这里你调用函数,但从不捕获它返回的输出

overTime(hw, r_p)

另外,你不想在无效输入上打破输入循环。

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