Question: Create a program that allows the user to enter 10 different integers. If the user tries to enter an integer that has already been entered, the program will alert the user immediately and prompt the user to enter another integer. When 10 different integers have been entered, the average of these 10 integers is displayed.
This is my code:
mylist = []
number = int(input("Enter value: "))
mylist.append(number)
while len(mylist) != 10:
number = int(input("Enter value: "))
if number in mylist:
number = int(input("The number is already in the list, enter another number: "))
mylist.append(number)
else:
mylist.append(number)
print(sum(mylist)/float(len(mylist)))
This kind of works but I need to create a loop that will keep on asking the user for another number if the number is in the array. Can you help?
asked Jun 18, 2015 at 0:43
James Ocean
1551 gold badge2 silver badges14 bronze badges
-
You are still appending the number the list even though it is already in the list.user1905595– user19055952015年06月18日 05:03:34 +00:00Commented Jun 18, 2015 at 5:03
1 Answer 1
What about:
mylist = []
number = int(input("Enter value: ")) mylist.append(number)
while len(mylist) != 10:
number = int(input("Enter value: "))
while number in mylist:
number = int(input("The number is already in the list, enter another number: "))
mylist.append(number)
print(sum(mylist)/float(len(mylist)))
Sign up to request clarification or add additional context in comments.
Comments
lang-py