dimanche 7 novembre 2021

Issue when trying to add user validation

Trying to add validation to user input.

So the code is:

print ('Does this service require an SFP Module? (y/n): ')
while True:
    sfpreq = input()
    if sfpreq != 'y' or 'n':
        print("You must enter either y or n")
        continue
    else:
        break

So even when the user enters 'n' it returns the "print("You must enter either y or n")" and continues the loop.

I have tried setting the variabl manually to and also tried another convention I found on realpython and also removed the if statement from the while loop:

sfpreq = "n"
if sfpreq != 'y' or sfpreq != 'n':
   print("You must enter either y or n")
else:
   print("Test")

Again it just returns:

admin@MacBook-Pro Learning Folder % python3 test22.py
You must enter either y or n

Am I just missing something very fundamental here?

Aucun commentaire:

Enregistrer un commentaire