初学者和简单程序(Python)-无法解决-需要建议

这是我的新手,我正在学习Python,并且在我编写的一个简单程序中遇到一些处理错误的困难。它仅根据工作时间计算工资,并询问用户工作时间和工资率。超过40小时的工作时间适用奖金率。我在主try / except的except子句中遇到了麻烦(所以我相信)。在执行过程中不会引发任何错误,但是该程序只是循环并询问相同的两个问题,并且如果可以将字符串项转换为float以进行计算,则不会继续进行计算。如果无法转换,即用户输入的是字母而不是数字,则会出现错误消息,并再次询问问题,直到收到有效的输入为止,在这种情况下,转换为浮点数即可。我将包括所有代码以供审核。如果有人可以帮助初学者,将不胜感激。抱歉,如果不允许错误帮助,这对SO也是新的。谢谢

码:

def script():

    try:
        def main():
            basePay = float()
            payDue = float()
            payPerExtraHour = float()
            if (hoursWorked > 40.0):
                extraHours = float()
                extraHoursPay = float()
                extraHours = hoursWorked-40.0
                basePay = 40 * payPerHour
                payPerExtraHour = 1.5 * payPerHour
                extraHoursPay = payPerExtraHour*extraHours
                payDue = round((basePay + extraHoursPay),2)
            else:
                payDue = float(hoursWorked*payPerHour) 
            print('Your gross pay is R'+str(payDue)+'\n')

            #repeating script if desired:
            def getValidInput():            
                response = input('Please enter Y(Yes) or N(No)')
                if (response == 'Y'):
                        script()
                elif(response == 'N'):
                        print('\n')
                        print('Thank you for using Gross Pay Calculator')
                else:
                    condition = True
                    while (condition==True):
                        response = str(input('Invalid response received. Please enter Y(Yes) or N(No)'))
                        if (response == 'Y'):
                            script()
                        elif(response == 'N'):
                            print('\n')
                            print('Thank you for using Gross Pay Calculator\n')

            #prompt to repeat script:
            yesOrNo = str(input('Would you like to perform another calculation? (Y/N)\n'))
            if(yesOrNo=='Y'):
                print('\n')
                script()
            elif(yesOrNo=='N'):
                print('\n')
                print('Thank you for using Gross Pay Calculator\n')
            else:
                response = input('Please enter Y(Yes) or N(No)\n')
                if (response == 'Y'):
                    script()
                elif(response == 'N'):
                    print('\n')
                    print('Thank you for using Gross Pay Calculator')
                else:
                    getValidInput()

        def repeatQuestions():
            hoursWorked = input('Enter hours worked: (whole number)\n')
            payPerHour = input('Enter pay rate: (hourly rate in Rands(R))\n')
            while (hoursWorked != type(float) and payPerHour != type(float)):
                try:
                    hoursWorked = float(input('Enter hours worked: (whole number)\n'))
                    payPerHour = float(input('Enter pay rate: (hourly rate in Rands(R))\n'))
                except:
                    print('Error, please enter numeric input for each item\n')
                    if(hoursWorked == type(float) and payPerHour == type(float)):
                        main()

        hoursWorked = float(input('Enter hours worked: (whole number)\n'))
        payPerHour = float(input('Enter pay rate: (hourly rate in Rands(R))\n'))

    except:
        print('Error, please enter numeric input for each item\n')
        hoursWorked = input('Enter hours worked: (whole number)\n')
        payPerHour = input('Enter pay rate: (hourly rate in Rands(R))\n')
        while (hoursWorked != type(float) and payPerHour != type(float)):
            hoursWorked = input('Enter hours worked: (whole number)\n')
            payPerHour = input('Enter pay rate: (hourly rate in Rands(R))\n')
            if (hoursWorked == type(float) and payPerHour == type(float)):
                main()


script()
评论