-1

I find my prog's if-else does not loop, why does this happen and how can I fix it for checking?

my prog supposed that store user's inputs which must between 10 and 100, then delete duplicated inputs. examples: num=[11,11,22,33,44,55,66,77,88,99] result: `[22,33,44,55,66,77,88,99]

inList=[]    
for x in range(1,11):
    num = int(input("Please enter number "+str(x)+" [10 - 100]: "))

    if num >= 10 and num <= 100:
        inList.append(num)
    else:
        num = int(input("Please enter a valid number: "))

print(inList)

I found that the if-else only did once, so when I enter invalid num for the 2nd time, the prog still brings me to the next input procedure. What happen occurs?

Please enter number 1 [10 - 100]: 1
Please enter a valid number: 1
Please enter number 2 [10 - 100]:

Additionally, may I ask how can I check the inList for duplicated num, and then remove both of the num in the list?

Mayble
  • 1
  • 2

2 Answers2

2

I'd also suggest a while loop. However the while loop should only be entered if the first prompt is erraneous:

Consider this example:

inList = []

for x in range(1,11):
    num = int(input("Please enter number "+str(x)+" [10 - 100]: "))

    while not (num >= 10 and num <= 100):
        num = int(input("Please enter a valid number [10 - 100]: "))

    inList.append(num)

print(inList)

However, may I suggest something else:

This code creates a list of valid inputs ["10","11"...."100"] and if the input which by default is a string is not inside that list we ask for new input. Lastly we return the int of that string. This way we make sure typing "ashashah" doesn't throw an error. Try it out:

inList = []

valid = list(map(str,range(10,101)))

for x in range(1,11):

    num = input("Please enter number {} [10 - 100]: ".format(x))

    while num not in valid:
        num = input("Please enter a valid number [10 - 100]: ")

    inList.append(int(num))

print(inList)
Anton vBR
  • 18,287
  • 5
  • 40
  • 46
  • thank you very much! it works! I once thought that should I use int() for all input, but I understand now I could just do it in the list – Mayble Oct 31 '17 at 15:16
1

I'm no python expert, and I don't have an environment setup to test this, but I can see where your problem comes from.

Basically, inside your for loop you are saying

if num is valid then
    add to array
else
    prompt user for num

end loop

There's nothing happening with that second prompt, it's just prompt > end loop. You need to use another loop inside your for loop to get num and make sure it's valid. The following code is a stab at what should work, but as said above, not an expert and no test environment so it may have syntax errors.

for x in range(1,11):
    i = int(0)
    num = int(0)

    while num < 10 or num > 100:
        if i == 0:
            num = int(input("Please enter number "+str(x)+" [10 - 100]: "))
        else:
            num = int(input("Please enter a valid number: "))

        i += 1

    inList.append(num)

print(inList)
Scoots
  • 3,048
  • 2
  • 21
  • 33