为什么我的Python代码忽略了if语句,并且在我添加了答案“no”时没有退出?

izj3ouym  于 2023-01-06  发布在  Python
关注(0)|答案(4)|浏览(127)
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) + "%,")

当我回答"no"时,它仍然让我继续玩测验而不是退出。当人们回答no而不是yes时,我如何停止它的运行?

vsaztqbk

vsaztqbk1#

测验不会停止,因为您没有包含任何代码来在用户输入“no”时停止测验。要解决此问题,您可以在if语句之后添加一个else语句,以检查用户是否要玩。else语句应包含一个break语句以退出循环。
这里你可以这样做:

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
zpqajqem

zpqajqem2#

在第一个if之后,您应该在if内应用条件{您应该应用条件的内部块}。如果用户输入yes/Yes以外的任何内容,则if条件块将不会运行。

    • 代码:-**
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) + "%,")
    • 输出:**

测试用例1

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

测试用例2

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%,
rkue9o1l

rkue9o1l3#

您可以通过构造问题和答案列表而不是为每个问题和答案编写离散的代码块来使其更易于管理。
您需要询问用户是否愿意回答问题(或不愿意)。
所以...

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)}')

因此,如果你想添加更多的问题和答案,你只需要改变元组的列表,其余的代码不需要改变

izkcnapc

izkcnapc4#

看起来你是Python的初学者。要在用户输入“否”时退出程序,只需添加条件。
1.您可以使用OS模块完成此操作

import os
if playing.lower() == "yes":
    print("Let's play! ") 
    score = 0
if playing.lower() == "no":
    print("Okay, bye!")
    os._exit(0)

1.您还可以定义一个函数,并使用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()

相关问题