3

I am trying to get a conditioned while with or statements for an assignment for a game programming class. I am trying to use a loop something like this. They are not exactly like this, but it is the same conditions for the while statement.

tempstr = input("Type B ")
while tempstr != "B" or tempstr != "b":
     tempstr = input("Type anything ")

I have also tried.

tempstr = input("Type B ")
while tempstr == "B" or tempstr == "b":
     tempstr = input("Type anything ")

As well as.

 while tempstr == "B" or tempstr == "b":
      tempstr = input("Type anything ")

I've checked that tempstr is set to B or b, and they still continue to ask for an input instead of just ending the program.

2 Answers 2

3

Try

While True:
    tempstr = input("Type anything ")
    if tempstr.lower()  == 'b':
        break
Sign up to request clarification or add additional context in comments.

3 Comments

Why would we want to add a while True wrapper and break out of it when we already know the condition that should trigger the while to break?
@sberry if it's a game then the OP might need to check for other inputs as well
That does work. Thank you. Guess I need to rethink my code, and not make this same mistake.
0

From your question, i have understood that you want to get out of the program whenever B letter is typed and we can make it possible bye just using simple if statement.

Try this,

while True:    
  tempstr = input("Type B ")
  if tempstr == "B" or tempstr == "b":
    break


Type B d
Type B e
Type B g
Type B b

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.