如何在函数循环之外定义参数?

我正在尝试执行一个函数,如果给出正确的输入(如 while 语句中所示),该函数将重复。但是,如果我的输入语句位于我的函数之上,我的程序会在附加循环中返回相同的值,而无法选择使用 newhrs和输入。rate如果它们在当前呈现的函数内,则表明 my(hrs,rate)未定义。如何定义hrs和rate参数,同时将我的输入保留在函数内?


def CalPay(hrs,rate):

    hrs = input('Please enter number of hours worked for this week:')

    rate = input('What is hourly rate:')

    try:

        hrs = float(hrs)

    except:

        hrs = -1

    try:

        rate = float(rate)

    except:

        rate = -1

    if hrs <= 0 :

        print('You have entered wrong information for hours.')

    elif rate <= 0 :

        print('You have entered wrong rate information.')

    elif hrs <=  40 :

        pay = hrs * rate

        print ('Your pay for this week is:', pay)

    elif hrs > 40 and hrs < 60 :

        pay = ((hrs - 40) * (rate * 1.5)) + (40 * rate)

        print ('Your pay for this week is:', pay)

    elif hrs >= 60 :

        pay = ((hrs - 60) * (rate * 2.0)) + (20 * (rate * 1.5)) + (40 * rate)

        print ('Your pay for this week is:', pay)


while True:

    CalPay(hrs,rate)

    yn = input('Do you wish to repeat this program? (y/n)').lower()

    if yn == 'y' :

        continue 

    if yn == 'n' :

        break

print ('Done!')


汪汪一只猫
浏览 107回答 2
2回答

智慧大石

首先,您需要在函数之后初始化这两个变量,因此每当您输入变量的值时,都必须定义它们。像这样的东西:# those two lines will be after the functionhrs = 0&nbsp; &nbsp;rate = 0完整的程序将如下所示:-def CalPay(hrs,rate):&nbsp; &nbsp; hrs = input('Please enter number of hours worked for this week:')&nbsp; &nbsp; rate = input('What is hourly rate:')&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; hrs = float(hrs)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; hrs = -1&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; rate = float(rate)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp; &nbsp; rate = -1&nbsp; &nbsp; if hrs <= 0 :&nbsp; &nbsp; &nbsp; &nbsp; print('You have entered wrong information for hours.')&nbsp; &nbsp; elif rate <= 0 :&nbsp; &nbsp; &nbsp; &nbsp; print('You have entered wrong rate information.')&nbsp; &nbsp; elif hrs <=&nbsp; 40 :&nbsp; &nbsp; &nbsp; &nbsp; pay = hrs * rate&nbsp; &nbsp; &nbsp; &nbsp; print ('Your pay for this week is:', pay)&nbsp; &nbsp; elif hrs > 40 and hrs < 60 :&nbsp; &nbsp; &nbsp; &nbsp; pay = ((hrs - 40) * (rate * 1.5)) + (40 * rate)&nbsp; &nbsp; &nbsp; &nbsp; print ('Your pay for this week is:', pay)&nbsp; &nbsp; elif hrs >= 60 :&nbsp; &nbsp; &nbsp; &nbsp; pay = ((hrs - 60) * (rate * 2.0)) + (20 * (rate * 1.5)) + (40 * rate)&nbsp; &nbsp; &nbsp; &nbsp; print ('Your pay for this week is:', pay)hrs = 0rate = 0while True:&nbsp; &nbsp; CalPay(hrs,rate)&nbsp; &nbsp; yn = input('Do you wish to repeat this program? (y/n)').lower()&nbsp; &nbsp; if yn == 'y' :&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;&nbsp; &nbsp; if yn == 'n' :&nbsp; &nbsp; &nbsp; &nbsp; breakprint ('Done!')输出Please enter number of hours worked for this week: 36What is hourly rate: 6Your pay for this week is: 216.0Do you wish to repeat this program? (y/n)YPlease enter number of hours worked for this week: 12What is hourly rate: 5Your pay for this week is: 60.0Do you wish to repeat this program? (y/n)

哈士奇WWW

在 while 循环中,您调用CalPay并传入hrsand rate。您正在调用一个函数并为其提供在函数内部创建的两个值,即当您调用 时它们不存在CalPay,因此您会收到错误。只需在 while 循环中收集输入,而不是在函数内部收集输入。像这样:while True:&nbsp; &nbsp; hrs = input('Please enter number of hours worked for this week:')&nbsp; &nbsp; rate = input('What is hourly rate:')&nbsp; &nbsp; CalPay(hrs,rate)&nbsp; &nbsp; yn = input('Do you wish to repeat this program? (y/n)').lower()&nbsp; &nbsp; if yn == 'y' :&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;&nbsp; &nbsp; if yn == 'n' :&nbsp; &nbsp; &nbsp; &nbsp; breakprint ('Done!')注意:您必须相应地调整重复程序的逻辑。另一个更好的解决方案是从 CalPay 和函数调用中删除参数,然后收集函数内所需的信息。正如阿努拉格所提到的。def CalPay():&nbsp; &nbsp; hrs = input('Please enter number of hours worked for this week:')&nbsp; &nbsp; rate = input('What is hourly rate:')&nbsp; &nbsp; try:&nbsp; &nbsp; &nbsp; &nbsp; hrs = float(hrs)&nbsp; &nbsp; except:&nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp;.&nbsp; &nbsp; &nbsp;.while True:&nbsp; &nbsp; CalPay()&nbsp; &nbsp; yn = input('Do you wish to repeat this program? (y/n)').lower()&nbsp; &nbsp; if yn == 'y' :&nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp;&nbsp; &nbsp; if yn == 'n' :&nbsp; &nbsp; &nbsp; &nbsp; breakprint ('Done!')
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python