0

Not sure why even when the entry is correct I still get the error message before it accepts the correct input.

even_num=int(input("Please enter a even number from 1-20: "))

while even_num != "2" and even_num !="4" and even_num != "6" and even_num != '8' and 
even_num != '10' and even_num != '12' and even_num != '14' and even_num != '16' and 
even_num != '18' and even_num !='20':
    
    even_num= input( 'Entry invalid.Please enter a even number: ')
print('Entry intput: ',even_num,'Entry Accepted')
Lia
  • 13
  • 2
  • what error message? what are you trying to do? – Alexander May 19 '22 at 22:48
  • You are converting to an int and comparing to a string. – Andras Deak -- Слава Україні May 19 '22 at 22:49
  • The first time you assign `even_num`, you have applied `int()` to it - so it cannot possibly be equal to `"2"` or any of the other *strings* you are comparing it to. The reassignment of `even_num` inside the loop omits the `int()`, so it's now possible to enter a matching string. – jasonharper May 19 '22 at 22:49
  • Please [edit] your question and always post "the error message" you got. It helps us to spot the error and explain it, or even guide you interpreting the error-message. – hc_dev May 19 '22 at 23:38
  • 1
    The code currently shown in your question isn't valid Python — it causes a`SyntaxError: invalid syntax` because you cannot split the `while` condition up over multiple lines like that. Is that what the error message says? – martineau May 20 '22 at 00:00

2 Answers2

1

The following will check entries for validity and accept valid ones on the first try:

while True:
    try:
        even_num = int(input("Please enter an even number from 1-20: "))
    except ValueError:
        print("Sorry, please enter an integer value.\n")
        continue

    if not 1 <= even_num <= 20:
        print("Sorry, your response was not in the range of 1-20.\n")
        continue
    elif even_num % 2 != 0:
        print("Sorry, your response must be an even number.\n")
        continue
    else:
        break

print('Entry input: ', even_num, 'accepted')

martineau
  • 119,623
  • 25
  • 170
  • 301
0

Without given error-message I can only suggest improvement:

  1. simplify the test for even number with in [2,4] (compare to a list of integers instead combining string-comparison)
  2. extract the test as function and make it defensive/safe (test also for int)
  3. improve readability of the loop-condition similar to "ask while input not valid"
  4. format strings (use f-string since Python 3.8) and give user a feedback which input was invalid
  5. reuse the prompt when asking for input
# extract condition to a function
def valid_even_num(number):
    # test for int  and number is even (in valid range)
    if isinstance(number, int) and number in [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
        return True
    else
        return False


# can reuse the prompt
prompt = "Please enter an even number from 1-20: "

# following input returned to n can also be a string 
n = input(prompt)

# test what happens if no number is entered. Does int(n) raise an error?
while not valid_even_num(int(n)):
    print(f"Entry '{n}' is invalid!\n")
    n = input(prompt)

print(f"Entry {n} accepted.")

For advanced input-retries see Asking the user for input until they give a valid response. The answer there explains the same pattern that Martineau answered: while True: try/except/continue.

hc_dev
  • 8,389
  • 1
  • 26
  • 38