0

I ask the user to enter a list of 4 numbers and I have to verify that they are all different (if not he has to do it again), however it is a piece of a bigger homework and I can only use simple/basic code, meaning I can't use sets or any function, only lists, loops ...

So I asked the user to enter 1 element at a time to compare them everytime with the ones already entered but I guess there is a much easier way to do so. My program looks like :

code= input ("enter a list of four distinct numbers between 1 and 8:")
for c in code:
while type (code)!=list or len(code)!=4 or type(c)!=int or code[c]<1 or code[c]>8 or ????????? :
     print "try again ! this is not correct"
     code=input("enter a list:")

How can I do that ? Thank you

Camille
  • 11
  • 2
  • Not sure how basic you need to be, but [Asking the user for input until they give a valid response](http://stackoverflow.com/questions/23294658/asking-the-user-for-input-until-they-give-a-valid-response) may be useful to you. – Kevin Oct 31 '14 at 15:27

1 Answers1

0

Here is a solution with them entering one value at a time, and you prompting to re-enter if it does not fit your criteria.

values = []  # initialize empty list

while len(values < 4):
    value = int(input("enter a number between 1 and 8:"))
    if value in values:  # Check if unique
        print("You already entered this")
    elif value < 1 or value > 8:  # Check bounds
        print("Value must be between 1 and 8")
    else:
        values.append(value)  # Valid number, add to your list

print(values)  # print out the final list
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218