1

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?

mstbaum
4342 silver badges11 bronze badges
asked Jun 18, 2015 at 0:43
1
  • You are still appending the number the list even though it is already in the list. Commented Jun 18, 2015 at 5:03

1 Answer 1

2

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)))
answered Jun 18, 2015 at 0:47
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.