![]() |
sum of lisit with no inputs - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: sum of lisit with no inputs (/thread-24793.html) Pages:
1
2
|
RE: sum of lisit with no inputs - ndc85430 - Mar-08-2020 You only seem to be defining those variables when some conditions are satisfied (i.e. those tested in the if statements on lines 27 and 36), yet the call to hmrc on line 40, which needs those variables happens unconditionally. How do you expect that to work?
RE: sum of lisit with no inputs - Chuck_Norwich - Mar-08-2020 Line 28 RE: sum of lisit with no inputs - Larz60+ - Mar-08-2020 after line 23 new_contractor_payment = input('New entry? Y/N ').upper() add: cis_total_1 = None
RE: sum of lisit with no inputs - ndc85430 - Mar-08-2020 This is sort of what I was getting at. The problem, though, is that the hmrc function expects floats, so if the relevant branches aren't entered to set numeric values, the program will throw an exception when trying to use those None s (line 19). In any case, you either need to provide a sensible default value, or if there isn't one, handle that and fail gracefully.
RE: sum of lisit with no inputs - Chuck_Norwich - Mar-08-2020 Ok, thanks for replies. Not had time to play with new info yet. Making more sense to me now. RE: sum of lisit with no inputs - Chuck_Norwich - Mar-08-2020 Ok, thank you. Now I get it. RE: sum of lisit with no inputs - Chuck_Norwich - Mar-09-2020 If anyone interested: Solved in the final few lines by taking the summing of the lists and the resultant variables outside the functions and appending a 0 to each list so the operator has something to work with. rmes_cis_add_list = [] cis_add_list = [] def contractor_deets(): labour = float(input("Labour:\n")) cis_deducted = float(labour) * 0.2 cis_add_list.append(cis_deducted) print(cis_add_list) def rmes_cis_deductions(): rmes_labour = float(input("Labour:\n")) rmes_cis_deducted = float(rmes_labour) * 0.2 rmes_cis_add_list.append(rmes_cis_deducted) print(rmes_cis_add_list) def hmrc(cis_total_1,cis_total_2): hmrc_payment = float(input('Payment to HMRC:\n ')) hmrc_credit = float(input('Credit with HMRC:\n ')) hmrc_due = float(hmrc_payment)+float(hmrc_credit)+float(cis_total_1 - cis_total_2) print(hmrc_due) new_contractor_payment = input('New entry? Y/N ').upper() while new_contractor_payment == 'Y': contractor_deets() new_contractor_payment = input('New entry? Y/N ').upper() if new_contractor_payment != 'Y': break rmes_new_cis_deduct = input('Suffered CIS deduction? Y/N ').upper() while rmes_new_cis_deduct == 'Y': rmes_cis_deductions() new_cis_deduct = input('New CIS deduction? Y/N ').upper() if new_cis_deduct != 'Y': break cis_add_list.append(0) cis_total_1 = sum(cis_add_list) rmes_cis_add_list.append(0) cis_total_2 = sum(rmes_cis_add_list) hmrc(cis_total_1,cis_total_2) RE: sum of lisit with no inputs - Larz60+ - Mar-09-2020 Thank you for sharing. |