i apologise for the longevity of this question
I want to write a piece of code which essentially checks if the str name of an input has already been added within a txt file, let me tell you this scenario:
I am using Classes and Listing in order to structure my data (stored in a txt file) for comparisons later, I am using the code that follows:
import csv
class Volunteer:
def __init__(self,name,coin_type,weight_of_bag,true_count):
self.name = name
self.coin_type = coin_type #a function allowing me to class the data
self.weight_of_bag = weight_of_bag
self.TrueCount = true_count
with open("CoinCount.txt","r+") as csvfile: #opens file as CSV *IMPORTANT*
volunteers = []
for line in csvfile:
volunteers.append(Volunteer(*line.strip().split(',')))
I have also created some more simple code such as a menu etc, within this menu though I want to include the ability to input new volunteers, for now i havent actually gotten to the stage of appending new data to the txt file however, i am at the moment trying to create an inspection which checks if the inputted volunteers name has already been used before, just for more information, i will send the data within the txt file:
Abena,5p,325.00,Y
Malcolm,1p,3356.00,Y
Jane,£2,120.00,Y
Andy,£1,166.25,N
Sandip,50p,160.00,Y
Liz,20p,250.00,Y
Andy,20p,250.00,Y
Andy,50p,160.00,Y
Jane,£1,183.75,N
Liz,£,179.0,N
Liz,50p,170.0,N
Jane,50p,160.0,Y
Sandip,£1,183.0,N
Jane,£2,132.0,N
Abena,1p,3356.0,N
Andy,2p,250.0,N
Abena,£1,175.0,Y
Malcolm,50p,160.0,Y
Malcolm,£2,175.0,N
Malcolm,£1,175.0,Y
Malcolm,1p,356.0,Y
Liz,20p,250.0,Y
Jane,£2,120.0,Y
Jane,50p,160.0,Y
Andy,£1,175.0,Y
Abena,1p,359.56,N
Andy,5p,328.5,N
Andy,£2,108.0,N
Malcolm,£2,12.0,N
I will also give you the code for this part of the menu so that if you wish to explain to me how to fix it you will be able to use the same variable names
def open_menu_1():
inputvolunteername = input("Enter the volunteers name you wish to add: ")
if hasNumbers(inputvolunteername):
yes_or_no = input("volunteer name contains numbers, would you like to try again? ")
if yes_or_no == "yes" or "Yes":
print("")
open_menu_1()
else:
print("")
print("you have been sent back to the main menu")
print("")
open_menu()
elif any(not c.isalnum() for c in inputvolunteername):
yes_or_no = input("volunteer name contains special characters, would you like try again? ")
if yes_or_no == ("yes" or "Yes"):
open_menu_1()
else:
print("")
print("you have been sent back to the main menu")
print("")
elif inputvolunteername == volunteers[0].name:
print("oi")
I designed a tiny grain of the code i would need to test out if it would work,that is displayed at the bottom of the code above
i essentially ran the program and typed in as the input the first name on the txt file (Abena) and by doing so, the console printed "oi". I thought this would be as difficult as this part of the code got as i thought all i would have to do now is replace
elif inputvolunteername == volunteers[0].name:
print("oi")
with
elif inputvolunteername == volunteers[0:].name:
print("oi")
i thought this would work because in lists this usually would go through all from 0 to infinity until there was nothing else to search from that name class category, however in actuality once i ran the program i was greeted with the following message:
elif inputvolunteername == volunteers[0:].name: AttributeError: 'list' object has no attribute 'name'
this confused me as I do have an attribute 'name' and that is made evident by the fact that that the other line of code which i tested out did work however by simply adding a colon it says that there is no attribute,
please let me know what i need to change to the bottom line of code in order to make the program detect whether the inputvolunteername is equal to any of the names already written on the txt file.