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

标签 python python-3.x

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

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

最佳答案

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

类似的东西:

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

关于python - 如何在函数循环之外定义参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64431874/

相关文章:

python - 计算文本文件中的单词

python - 将 pandas DataFrame 列与系列相乘

python - Pygame绘制抗锯齿粗线

python - 如何将实时视频帧从 ffmpeg 传输到 PIL?

python - 从文本文件中按列打印列表

Python:如何在我的时间序列中删除每天的前 5 分钟?

python - 列出 pandas.read_sql 中的 sql 表

python - 使用 join 在 python 中创建字典

python - Strawpoll.me API 对于我的 Python 程序返回 "400 Bad Request",但可以在终端上运行

python-3.x - 如何对多个属性进行机器学习(树)?