smallmac
smallmac

Reputation: 11

Python break function doesn't end while true

Why will the break not end the while true and return to the start?

while True:
    print('This is a quiz')
    print('What is your name?')
    Name = input()
    print('Hello ' + Name + ', The quiz will now begin')
    import time
    time.sleep(2)
    question1 = "Question one: "
    answer1 = "True" and "true"
    print(question1)
    qanswer = input()

    if qanswer != answer1:
        print('Sorry, the answer is: ' + answer1)
        break

    if answer1 == qanswer:
            print("Correct! Here's the next question")

I'm pretty new to python so I assume it's just a simple misuse of the terms.

Upvotes: 1

Views: 7679

Answers (3)

Davoud Taghawi-Nejad
Davoud Taghawi-Nejad

Reputation: 16826

....
answer1 = ["True", "true"]
...
if not(qanswer in answer1):
    print('Sorry, the answer is: ' + answer1)
    break
else:
   print("Correct! Here's the next question")

Upvotes: 0

Yuval Adam
Yuval Adam

Reputation: 165312

break exists the entire while loop.

continue is what you're looking for, returning to the next while iteration.

You can read more about control flow tools, break and continue in the official docs: http://docs.python.org/2/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

(You also have some other bugs as others have mentioned correctly)

Upvotes: 5

Foggzie
Foggzie

Reputation: 9821

use the "continue" keyword, not "break".

Upvotes: 0

Related Questions