1

i wrote here a loop which asks the user if he want to repeat a block of instructions. And wanted to give the user three tries to choose what he wants (Yes / No) if he doesn't the loop end. This is my code:

# Ask for repeating
i = 0
def question():
    global i
    while True:
        print("Give another try?")
        answer = input("(Y/N): ")
        answer = answer.upper()
        if answer == 'Y':
            main()
        elif answer == 'N':
            print("Thank you for participating")
            break # here it works when 'n' is typed
        else: # i count how much the while loop id repeated
            if (i < 2):
                i += 1
                question()
            else: # else is executed when i == 2
                print("don't Play with us!!")
                break # here it doesn't work.

I want help.

0

2 Answers 2

1
x = True
i = 0
def question():
    # Ask for repeating
    global i
    global x
    while x is True:
        print("Give another try?")
        answer ='a'
        answer = answer.upper()
        if answer == 'Y':
            pass
        elif answer == 'N':
            print("Thank you for participating")
            break  # here it works when 'n' is typed
        else:  # i count how much the while loop id repeated
            if (i < 2):
                i += 1
                question()
            else:  # else is executed when i == 2
                print("don't Play with us!!")
                x = False  # here it doesn't work.
Sign up to request clarification or add additional context in comments.

Comments

1

Add another break outside the last else block. Your code should be

i = 0
def question():
    global i
    while True:
        print("Give another try?")
        answer = input("(Y/N): ")
        answer = answer.upper()
        if answer == 'Y':
            main()
        elif answer == 'N':
            print("Thank you for participating")
            break # here it works when 'n' is typed
        else: # i count how much the while loop id repeated
            if (i < 2):
                i += 1
                question()
            else: # else is executed when i == 2
                print("don't Play with us!!")
                break # here it doesn't work.
            break

1 Comment

ohh and this is my fault: break should be outside the second 'else'.

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.