首页 > 解决方案 > 当玩家回答三个问题时,是否可以停止测验

问题描述

我在测验中添加了“生活”功能。当玩家答错三个问题时,意味着生命=0。游戏应该结束,但相反,它会继续并将零传递到 -1,依此类推。

我尝试了一个while循环,但我不确定我是否正确编码它,因为它无法工作。

lives = 3
print ("lives =", lives)
print (" ")

name = input ("What is your name?")
print ("Hello",name,". Good Luck.")


while lives >= 0:
    ##is the player ready to start
    play = input ("Would you like to start? (Type Y for yes and N for no)")
    if play == "Y" or play == "y":
        from time import sleep
        sleep (1.0)
        print("Starting in...")
        sleep (1.0)
        print("3")
        sleep(1.0)
        print("2")
        sleep(1.0)
        print("1")
        break
    ##3, 2, 1 countdown added wk.4 friday
    elif play == "N" or play == "n":
        print ("End")
        break
    else:
        print ("That is not an answer.\n")


## 1st Question
question1 = ("1. What is Brisbanes AFL team called?")
options1 = (" a. Brisbane Tigers \n b. Brisbane Lions \n c. Brisbane Broncos \n d. Brisbane Magpies")
print (question1)
print (options1)
answer = input (">")
if answer == "B" or answer == "b":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives-=1


## 2nd Question
question2 = ("2. What sport did Greg Inglis play")
options2 = (" a. rugby league \n b. rugby union \n c. AFL \n d. Soccer")
print (question2)
print (options2)
answer = input (">")
if answer == "A" or answer == "a":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives-=1

## 3rd Question
question3 = ("3. Where were the 2018 Commonwealth Games held?")
options3 = (" a. Sunshine Coast \n b. Melbourne \n c. Brsbane\n d. Gold coast")
print (question3)
print (options3)
answer = input (">")
if answer == "D" or answer == "d":
    print ("Correct!")
else:
    print("Incorrect.")
    print ("lives =", lives - 1)
    lives-=1


游戏应该停止,而是继续说玩家处于消极状态。我将不胜感激,并提前感谢您抽出时间帮助我解决问题。如果您有任何其他建议可以比我的测验更好,请随时发表评论。

标签: pythonpython-3.x

解决方案


如果你想在生活 = 0 的情况下重新开始游戏,那么你还需要将所有问题都包含在 while 循环中。并且还将 live var 包含到 while 循环中,因此每次代码进入循环时都会设置它,例如:

lives = 3
print ("lives =", lives)
print (" ")

name = input ("What is your name?")
print ("Hello",name,". Good Luck.")


while lives >= 0:
    lives = 3
    ##is the player ready to start
    play = input ("Would you like to start? (Type Y for yes and N for no)")
    if play == "Y" or play == "y":
        print ('playing')
    ##3, 2, 1 countdown added wk.4 friday
    elif play == "N" or play == "n":
        print ("End")
        break
    else:
        print ("That is not an answer.\n")


    ## 1st Question
    question1 = ("1. What is Brisbanes AFL team called?")
    options1 = (" a. Brisbane Tigers \n b. Brisbane Lions \n c. Brisbane Broncos \n d. Brisbane Magpies")
    print (question1)
    print (options1)
    answer = input (">")
    if answer == "B" or answer == "b":
        print ("Correct!")
    else:
        print("Incorrect.")
        print ("lives =", lives - 1)
        lives-=1


    ## 2nd Question
    question2 = ("2. What sport did Greg Inglis play")
    options2 = (" a. rugby league \n b. rugby union \n c. AFL \n d. Soccer")
    print (question2)
    print (options2)
    answer = input (">")
    if answer == "A" or answer == "a":
        print ("Correct!")
    else:
        print("Incorrect.")
        print ("lives =", lives - 1)
        lives-=1

    ## 3rd Question
    question3 = ("3. Where were the 2018 Commonwealth Games held?")
    options3 = (" a. Sunshine Coast \n b. Melbourne \n c. Brsbane\n d. Gold coast")
    print (question3)
    print (options3)
    answer = input (">")
    if answer == "D" or answer == "d":
        print ("Correct!")
    else:
        print("Incorrect.")
        print ("lives =", lives - 1)
        lives-=1

推荐阅读