Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
sum of lisit with no inputs
#11
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?
Reply
#12
Line 28
Reply
#13
after line 23 new_contractor_payment = input('New entry? Y/N ').upper()
add:
cis_total_1 = None
Reply
#14
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 Nones (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.
Reply
#15
Ok, thanks for replies.
Not had time to play with new info yet. Making more sense to me now.
Reply
#16
Ok, thank you. Now I get it.
Reply
#17
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)
Reply
#18
Thank you for sharing.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020