Home > Mobile >  Why is my Python code ignoring my if-statement and not quitting when I add the answer "no"
Why is my Python code ignoring my if-statement and not quitting when I add the answer "no"

Time:01-06

print("Welcome to my quiz page!")
playing = input("Do you want to play? ")
if playing.lower() == "yes":   #when people answer no here, the quiz should stop but it doesn't. why?   
    print("Let's play! ")
    score = 0

answer = input("What is the capital of Japan? ")
if answer.lower() == "tokyo":
    print("Correct!")
    score  = 1
else:
    print("Incorrect!")

answer = input("What is a female Japansese dress called? ")
if answer.lower() == "kimono":
    print("Correct!")
    score  = 1
else:
    print("Incorrect!")

answer = input("What are Japanese gang members called? ")
if answer.lower() == "yakuza":
    print("Correct!")
    score  = 1
else:
    print("Incorrect!")

print("You've got "   str(score)   " questions correct!")
print("You've got "   str((score / 3) * 100)   "%,")

When I answer "no" it still let me carry on playing the quiz instead of quitting. How can I stop it running when people answer no instead of yes?

CodePudding user response:

The quiz does not stop because you have not included any code to stop the quiz when the user enters "no". To fix this, you can add an else statement after the if statement that checks if the user wants to play. The else statement should contain a break statement to exit the loop.

Here How you can do so:

print("Welcome to my quiz page!")

while True:
    playing = input("Do you want to play? ")
    if playing.lower() == "yes" or playing.lower() == "y":
        print("Let's play! ")
        score = 0

        answer = input("What is the capital of Japan? ")
        if answer.lower() == "tokyo":
            print("Correct!")
            score  = 1
        else:
            print("Incorrect!")

        answer = input("What is a female Japansese dress called? ")
        if answer.lower() == "kimono":
            print("Correct!")
            score  = 1
        else:
            print("Incorrect!")

        answer = input("What are Japanese gang members called? ")
        if answer.lower() == "yakuza":
            print("Correct!")
            score  = 1
        else:
            print("Incorrect!")
        
        print("You've got "   str(score)   " questions correct!") 
        print("You've got "   str((score / 3) * 100)   "%,")
    else:
        break

CodePudding user response:

After the first if you should apply condition inside if{you should apply inner block of condition}.. if user input anything else than yes/Yes the if condition block will not run..!

Code:-

print("Welcome to my quiz page!")
playing = input("Do you want to play? ")
score = 0
if playing.lower() == "yes":   #when people answer no here, the quiz should stop but it doesn't. why?   
    print("Let's play! ")
    

    answer = input("What is the capital of Japan? ")
    if answer.lower() == "tokyo":
        print("Correct!")
        score  = 1
    else:
        print("Incorrect!")
    
    answer = input("What is a female Japansese dress called? ")
    if answer.lower() == "kimono":
        print("Correct!")
        score  = 1
    else:
        print("Incorrect!")
    
    answer = input("What are Japanese gang members called? ")
    if answer.lower() == "yakuza":
        print("Correct!")
        score  = 1
    else:
        print("Incorrect!")

    print("You've got "   str(score)   " questions correct!")
    print("You've got "   str((score / 3) * 100)   "%,")

Output:

Testcase1

Welcome to my quiz page!
Do you want to play? no
> #If block not run. 

Testcase2

Welcome to my quiz page!
Do you want to play? yes
Let's play! 
What is the capital of Japan? tokyo
Correct!
What is a female Japansese dress called? yakuza
Incorrect!
What are Japanese gang members called? yakuza
Correct!
You've got 2 questions correct!
You've got 66.66666666666666%,

CodePudding user response:

You can make this more manageable by constructing a list of questions and answers rather than writing discrete blocks of code for each.

You need to be asking the user if they want to answer questions (or not).

So...

Q_and_A = [
    ('What is the capital of Japan', 'Tokyo'),
    ('What is a female Japansese dress called', 'kimono'),
    ('What are Japanese gang members called', 'yakuza')
]

score = 0

for q, a in Q_and_A:
    if input('Would you like to try to answer a question? ').lower() in {'n', 'no'}:
        break
    if input(f'{q}? ').lower() == a.lower():
        print('Correct')
        score  = 1
    else:
        print('Incorrect')

print(f'Your total score is {score} out of a possible {len(Q_and_A)}')

Thus if you want to add more questions and answers you just change the list of tuples. The rest of the code doesn't need to be changed

CodePudding user response:

It seems you are a beginner in python. To exit the program when user enters "no" just you have to add the condition..

  1. You can do this using OS module
import os
if playing.lower() == "yes":
    print("Let's play! ") 
    score = 0
if playing.lower() == "no":
    print("Okay, bye!")
    os._exit(0)
  1. You can also define a function and make it happen using raise SystemExit
    def check():
        print("Welcome to my quiz page!")
        playing = input("Do you want to play? ") 
        if playing.lower() == "yes":
            print("Let's play! ") 
            score = 0
        if playing.lower() == "no":
            print("Okay, bye!")
            raise SystemExit
    check()
  • Related