1
serviceType = ""

while serviceType != "R" or serviceType != "P":
    serviceType = input("Service type (R/P): ").upper()
    print(serviceType)
    if serviceType != "R" or serviceType != "P":
        print("Error: Invalid Entry \n")

Whenever I run the code above, the output, no matter what input I put into it, is the message "Error: Invalid Entry". Is there something I am doing wrong?

3 Answers 3

4

One of the conditions in your if condition is always True, therefore you always get the error.

Try this:

serviceType = ""

while serviceType != "R" and serviceType != "P":
    serviceType = input("Service type (R/P): ").upper()
    print(serviceType)
    if serviceType not in ["R", "P"]: # <- line changed
        print("Error: Invalid Entry \n")
Sign up to request clarification or add additional context in comments.

7 Comments

or even easier, just change the condition from or to and in the if statement+do the same for the while loop if you assume that you need to exit the loop when serviceType is "R" or "P"
I don't believe this works either. It no longer prints out the error message but it also just keeps rerunning the same code over and over again
actually wait Alex.Kh's response worked. Thanks for the help!
@temp116055 glad it was helpful. I hope you got the gist of the problem. This condition was always True as your input will always be either not "R" or not "P". Thus, you are validating that neither are correct at the same time, which needs and
just did, thank you all for the help
|
1

Here's a more typical structure for this kind of pattern:

while True:
    serviceType = input('Service type (R/P): ').upper()
    if serviceType in ('R', 'P'):
        break # serviceType is either R or P
    print('Error: Invalid entry')

print(serviceType) # use the validated serviceType here

Comments

0

As per my understanding you want to build a program which takes service type from user and if the service type is not R or P, it return an invalid entry message and keep taking user input until user enter R or P. The following code works for me.

serviceType = ""

while serviceType != "R" and serviceType != "P":
    serviceType = input("Service type (R/P): ").upper()
    print(serviceType)
    if serviceType != "R" and serviceType != "P":
        print("Error: Invalid Entry \n")

Comments

Your Answer

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.