-2
def checkclas(x):
   while x == "A" or "B" or "C" is not True:
      x = input("You have to choose one of the three classes: ").upper() 
      return x

clas = input("which class are you in? ").upper()
clas = checkclas(clas)
print (clas)

I tried to solve the problem with this code, but I couldn't. I want the program to check if the input class is valid, and then to print it, but it keeps going in the while loop forever, even though I input the right class. I think the problem is with the (or) statement. If anyone has got a solution please help me, this is for my computer science assessment, and the code has to be efficient.

Y.Hasan
  • 11
  • 7
  • `while x not in "ABC":`. –  Jan 28 '16 at 21:43
  • 1
    Possible duplicate of [How do I test one variable against multiple values?](http://stackoverflow.com/questions/15112125/how-do-i-test-one-variable-against-multiple-values) – user2357112 Jan 28 '16 at 21:43
  • Not that your current statement evaluates as: x == "A", or "B", or "C" is not True. With your way, it should have been `(x == "A" or x == "B" or x == "C") is not True`. –  Jan 28 '16 at 21:44
  • Thank you for your comment, but I have tried your solution it is not working. – Y.Hasan Jan 28 '16 at 21:47
  • "It is not working" is very vague. The more you tell us, the more we can help. Also, you don't have to say `is not True` its redundent. You can just say `while not x == "A" or x == "B" or x == "C":`to get the same effect of your post – AustinWBryan Jan 28 '16 at 21:51
  • @AustinWBryan In your solution only when I type (A) it breaks the loop, but when I type (B) or (C), it will continue. – Y.Hasan Jan 28 '16 at 21:57
  • 1
    Thank you @AustinWBryan, I have developed your solution by making brackets around the while loop statment, and I have solved the problem. – Y.Hasan Jan 28 '16 at 22:03

2 Answers2

1

It looks like you need an if statement wrapped around your while statement. See the code below. (Note that the user input has to be in string format.)

def checkclas(x):
   if x not in ("A", "B", "C"):
      while x not in ("A", "B", "C"):
          x = input("You have to choose one of the three classes:     ").upper()
      return x
   else:
      return x

clas = input("which class are you in? ").upper()
clas = checkclas(clas)
print (clas)

EDIT:

Addressed redundant if-else statement and provided a more natural (and appropriate - see here) way to input strings.

def checkclas(x):
    while x not in ("A", "B", "C"):
        x = raw_input("You have to choose one of the three classes: ").upper()
    return x

clas = raw_input("which class are you in? ").upper()
clas = checkclas(clas)
print (clas)
Community
  • 1
  • 1
Imad Ali
  • 88
  • 5
  • 1
    There is some redundancy here. You're checking for `x not in ("A", "B", "C")` twice. Therefore, if OP were to add in "D", he would have to add it in two places, instead of just one. – AustinWBryan Jan 28 '16 at 22:06
  • Thank you @Imad Ali , your code is helped me to make my code more efficient. I have just deleted the if and else statements, because there was no need for it. – Y.Hasan Jan 28 '16 at 22:09
  • @AustinWBryan - Thanks for addressing the redundancy. – Imad Ali Jan 29 '16 at 00:23
  • @Y.Hasan - happy to help. – Imad Ali Jan 29 '16 at 00:23
-2

If you have control over the definitions of A, B, and C I recommend using Python Inheritance.

class MyBaseObject(object):
    pass

class A(MyBaseObject):
    pass

class B(MyBaseObject):
    pass

class C(MyBaseObject):
    pass

...
x = A()
if isinstance(x, MyBaseObject):
    ...
ThorSummoner
  • 16,657
  • 15
  • 135
  • 147
  • This is completely overkill. – AustinWBryan Jan 28 '16 at 22:05
  • @AustinWBryan I proposed this because OP said the input was choosing between classes, it seems OP was not speaking of [classes](https://docs.python.org/2/tutorial/classes.html), but rather about a set of strings. – ThorSummoner Jan 29 '16 at 06:17
  • My bad. I tried to undownvote you, but it said my vote is locked unless this post is edited. But yes, his "class" idea confused me as well, while reading another post. He should have been more clear. – AustinWBryan Jan 29 '16 at 07:46
  • @AustinWBryan no hard feelings :) – ThorSummoner Jan 29 '16 at 18:28