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

2024-05-15 23:55:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我在测验中增加了“生活”功能。当玩家答错三个问题时,意味着生命值等于0。游戏应该结束,但它会继续,并以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


游戏应该停止,但继续说,球员是在消极的生活。我将感谢任何帮助,并提前感谢您抽出时间来帮助我解决我的问题。如果你有任何其他建议,可以更好地我的测验,而不是随意评论


Tags: oranswernameinputplayifissleep
2条回答
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

如果你想在lives=0的情况下重新开始游戏,那么你需要将所有的静默也包括在while循环中。并且还将lives 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

相关问题 更多 >