为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

汪汪一只猫 2023-12-05 15:29:24
我正在尝试执行一个函数,如果给出正确的输入(如 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' :        breakprint ('Done!')
查看完整描述

2 回答

?
智慧大石

TA贡献1946条经验 获得超3个赞

首先,您需要在函数之后初始化这两个变量,因此每当您输入变量的值时,都必须定义它们。


像这样的东西:


# those two lines will be after the function

hrs = 0   

rate = 0

完整的程序将如下所示:-

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)



hrs = 0

rate = 0


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!')

输出


Please enter number of hours worked for this week: 36

What is hourly rate: 6

Your pay for this week is: 216.0

Do you wish to repeat this program? (y/n)Y


Please enter number of hours worked for this week: 12

What is hourly rate: 5

Your pay for this week is: 60.0

Do you wish to repeat this program? (y/n)


查看完整回答
反对 回复 2023-12-05
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

在 while 循环中,您调用CalPay并传入hrsand rate。您正在调用一个函数并为其提供在函数内部创建的两个值,即当您调用 时它们不存在CalPay,因此您会收到错误。只需在 while 循环中收集输入,而不是在函数内部收集输入。像这样:


while True:

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

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

    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!')

注意:您必须相应地调整重复程序的逻辑。


另一个更好的解决方案是从 CalPay 和函数调用中删除参数,然后收集函数内所需的信息。正如阿努拉格所提到的。


def CalPay():

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

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

    try:

        hrs = float(hrs)

    except:

     .

     .

     .


while True:

    CalPay()

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

    if yn == 'y' :

        continue 

    if yn == 'n' :

        break

print ('Done!')


查看完整回答
反对 回复 2023-12-05
  • 2 回答
  • 0 关注
  • 99 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信