1

This is my code :

#Choose Report
def chooseReport():
    print "Choose a report."
    while True:
        choice = raw_input("Enter A or B: ")
        if choice == 'a' or choice == 'A':
            reportA()
            break
        elif choice == 'b' or choice == 'B':
            reportB()
            break
        else:
            continue

When I input either a or b, it just asks me to "Enter A or B" again. It doesn't go to the functions its supposed to.

Any idea why is this?

6
  • 1
    try choice.lower().strip() Commented Jan 20, 2018 at 16:54
  • On a side note, the else: continue is totally redundant here. Commented Jan 20, 2018 at 16:56
  • Have you tried debugging your program to see what does choice have? Commented Jan 20, 2018 at 16:58
  • 2
    break will only break out of the if condition. Use a condition variable to break out the loop Commented Jan 20, 2018 at 16:58
  • 1
    Well maybe the issue is with reportA() or reportB() methods because the code works fine and breaks Commented Jan 20, 2018 at 17:02

6 Answers 6

2

The code is perfect, except a redundant else, as mentioned in the comment. Are you entering a (a + space) rather than simply a (a without space) ? The problem is in the input that you are providing and not in the code!

Sign up to request clarification or add additional context in comments.

Comments

2
def chooseReport():
    print "Choose a report."
    t=True                                        # t starts at true
    while t:
        choice = raw_input("Enter A or B: ")
        if choice == 'a' or choice == 'A':
            reportA()
            t=False               # t turns false to break out of loop
        elif choice == 'b' or choice == 'B':
            reportB()
            t=False

Try this. It keeps looping when t is true and stops when t is false. The problem might also be in reportA or reportB or how you are calling chooseReport.

2 Comments

What is wrong with his break statements? Am I missing something?
This does not work? Is there any problem with 't' variable scope?
2

The problem is in the raw_input(). It returns a string but maybe this string is "a " or "a\n" though you have entered "a" or "b".

I would do this:

def chooseReport():
    print "Choose a report."
    while True:
        choice = raw_input("Enter A or B: ")
        if "a" in choice or "A" in choice:
            reportA()
            break
        elif "b" in choice or "B" in choice: 
            reportB()
            break
        else:
            continue

Comments

2

Tried your code in the following script, it works fine both on Linux and on Windows.

def reportA():
    print "AAAAA"

def reportB():
    print "BBBBB"

#Choose Report
def chooseReport():
    print "Choose a report."
    while True:
        choice = raw_input("Enter A or B: ")
        if choice == 'a' or choice == 'A':
            reportA()
            break
        elif choice == 'b' or choice == 'B':
            reportB()
            break
        else:
            continue


chooseReport();

Comments

2

First, your code works fine, the most probably error is that you are writing a wrong input (e.g: with more characters). To solve that you could use "a" in choice or "A" in choice. But if it isn't working... keep reading.

It's seems that break isn't affecting the while loop, I don't have python 2 so I am not very sure why (in python 3 [after change raw_input to input and print to print()] your code works perfect). So you should use the condition of the while to break it.

while True work theorically for ever because each time the code is executed it checks the condition -True- and because it's true it keeps looping.
You could manipulate that condition in order to break the loop (don't allow execute again its code).

For example you could use this:

#Choose Report
def chooseReport():
    print "Choose a report."
    allow = True    # allow start as True so the while will work
    while allow:
        choice = raw_input("Enter A or B: ")
        if choice.lower().strip() == "a":    # This is better. .lower() make "A" > "a", and .strip() delete " a " to "a", and "a/n" to "a".
            reportA()
            allow = False   # allow now is False, the while won't execute again
        elif choice.lower().strip() == "b":
            reportB()
            allow = False   # allow now is False, the while won't execute again
        # The else is complete redundant, you don't need it

Comments

1

Code is fine. I think you call your chooseReport() function in a loop or your input has extra characters and if conditions didn't satisfied.

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.