What throws me is that it works as long as I add something to each list.
But if I add nothing to the list it doesn't work.
Still trying to get my head around the answers given - they may be correct but I don't know what to do with the info.
I will create a completely stripped down version of the program so the problem is clear - to me as well as anyone else.
This is the relevant code without everything else, and with extra print statements so I can follow the inputs and calculations.
global cis_add_list
global rmes_cis_add_list
rmes_cis_add_list = [0]
cis_add_list = [0]
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():
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':
cis_total_1 = sum(cis_add_list)
print(cis_total_1)
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':
cis_total_2 = sum(rmes_cis_add_list)
print(cis_total_2)
break
hmrc()
Works fine as long as there is at least one input each for the first two functions.
If you don't have any inputs then then:
NameError: name 'cis_total_1' is not defined.
So, bear with me if I am missing something obvious (maybe some nomenclature?)
1. Is cis_total_1 not defined because there is nothing in the list to sum?
2. Following on from what ndc8530 said about passing in the variables to the function as arguments gives me this in my mind:
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)
but something is missing, not correct or I have the wrong end of the stick.
Pointers?
Thanks in advance.
Ok. So if I change the code to have the variables as arguments like this:
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':
cis_total_1 = sum(cis_add_list)
print(cis_total_1)
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':
cis_total_2 = sum(rmes_cis_add_list)
print(cis_total_2)
break
hmrc(cis_total_1,cis_total_2)
It does the same as the previous version without making the lists global.
Progress.
But, if there is no input to the list, then still-
NameError: name 'cis_total_1' is not defined.
So how would I get around a zero sum list? Some if statement in the hmrc function. Kinda thinking aloud here.