I'm Trying to create a data-set from user input. I am trying to prevent duplicates for one of the fields. I ask the user to pick a letter that matches their name, their age, their gpa and major. I want to make sure the letters entered are unique, but I am not sure how to do this when writing directly to a .csv file.
here is what I have so far.
import csv
from colorama import Fore, Back, Style
with open('students2.csv', 'w+', newline='') as csvfile:
columnheaders = ['NAME','AGE','GPA','MAJOR']
writer = csv.DictWriter(csvfile, fieldnames=columnheaders)
writer.writeheader()
for i in range(0,10):
askname=input('Please select the letter that matches your name from the following: (A, B, C, D, E, F, G, H, I, J), ')
askage=input('Please enter your Age: ')
askgpa=input('Please enter your GPA: ')
askmajor=input('Please select your major from the following (CS, CET, CIS, CE) ')
writer.writerow({'NAME': askname,'AGE': askage,'GPA': askgpa,'MAJOR': askmajor})
print(Back.BLACK +'My name starts with the letter:', askname ,' and I am ', askage, 'years old. I study ', askmajor, 'and my GPA is: ', askgpa)
print(Style.RESET_ALL)
I know how to do this with a list,
namelist = []
while True:
#Input name
while True:
name = str(input('What is your name? '))
if name.upper() not in ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J'):
print("Please use (A, B, C, D, E, F, G, H, I, J).")
continue
if name in namelist:
print("This name has already been used.")
continue
else:
namelist.append(name)
break
But is it possible to do this without having to do it via a list and then convert it to .csv?
Any help will be appreciated it. Thanks in advance.