玩家回答错误三个问题时是否可以停止测验

时间:2019-05-20 10:53:30

标签: python python-3.x

我在测验中添加了“生活”功能。当玩家回答三个错误的问题时,表示生命= 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


游戏应该停止,但继续说玩家处于消极的生活。我将不胜感激,在此先感谢您抽出宝贵的时间来帮助我解决问题。如果您还有其他建议可以使我的测验胜于随意评论,那么该建议就是如此。

2 个答案:

答案 0 :(得分:0)

如果您想在生命= 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

答案 1 :(得分:0)

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

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



##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")

##3, 2, 1 countdown added wk.4 friday
elif play == "N" or play == "n":
    print ("End")

else:
    print ("That is not an answer.\n")


while lives >= 0: # <----
    ## 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:
        lives -= 1 # <----
        print("Incorrect.")
        print ("lives =", lives) # <----
        if lives == 0: #<---
            break

    ## 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:
        lives -= 1 # <----
        print("Incorrect.")
        print ("lives =", lives) # <----
        if lives == 0: #<---
            break


    ## 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:
        lives -= 1 # <----
        print("Incorrect.")
        print ("lives =", lives) # <----
        if lives == 0: #<---
            break