1

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.

2
  • 2
    answer1 = "True" and "true" This is not doing what you think it is. Try printing answer1 after you create the variable. If you want to compare the user input to two different strings, you need to compare the input against both strings. Documentation on and Commented Dec 19, 2012 at 22:48
  • I don't think this means what you think it means: answer1 = "True" and "true". Commented Dec 19, 2012 at 22:48

3 Answers 3

5

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)

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

Comments

0

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

Comments

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

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.