Completely new to Python so bear with me.
I created a list through raw_input:
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
Now I have to check if any item already exists, and if it does ask for another raw_input to add to list. I'm completely stumped. It's not looping with this; it just prints a anyway.I also then have to append new item to original list. Stumped.
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
listStr = raw_input("Enter an item: ")
itemList.append (listStr)
for a in itemList:
if a in itemList :
a = raw_input("Enter another number: ")
You can use a while
loop to keep asking for input until an item is entered that isn't already in the list. This could be improved, but it should get you started:
itemList = []
num = int (raw_input("Enter a number: "))
for i in range (num):
while True:
listStr = raw_input("Enter an item: ")
if listStr in itemList:
print('That item is already in the list')
else:
itemList.append(listStr)
break
Slightly better version:
itemList = []
num = int(raw_input("Enter a number: "))
for i in range(num):
listStr = raw_input("Enter an item: ")
while listStr in itemList:
print("That item already exists")
listStr = raw_input("Enter another number: ")
itemList.append(listStr)