I am trying to add 1 to my variable and then add this variable to a list so when I print out my list, the value of the variable is within it. However, my code prints out an empty list despite me adding the variable to it.
case_number = 0
case_numbers = []
issue = input('Has the issue been solved?').lower()
if issue == 'no':
case_number += 1
case_numbers+[int(case_number)]
print(case_numbers)
If you want to add a value to a growing list you need to use list.append() method. It adds the value to the end of the list, so you code should be:
case_number = 0
case_numbers = []
issue = input('Has the issue been solved?').lower()
if issue == 'no':
case_number += 1
case_numbers.append(int(case_number))
print(case_numbers)
Extra tip:
list.append() method adds value to end of list so if you add the list B into list A using append() then it will add the listB inside the listA like this listA.append(listB)= ["a","b","c",["d","e","f"]] so if you want to add the values as normal list you should use list.extend() method which will add the values of one list to another such that listA.extend(listB)=["a","b","c","d","e","f"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With