python - 当玩家回答三个问题时,是否可以停止测验
问题描述
我在测验中添加了“生活”功能。当玩家答错三个问题时,意味着生命=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
游戏应该停止,而是继续说玩家处于消极状态。我将不胜感激,并提前感谢您抽出时间帮助我解决问题。如果您有任何其他建议可以比我的测验更好,请随时发表评论。
解决方案
如果你想在生活 = 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
推荐阅读
- rabbitmq - 如何避免通过 values.yml 传递敏感数据(bitnami 图表)
- mysql - 错误:找不到模块“续集/类型”
- python - 如何使用 APScheduler 的 BackgroundScheduler() 停止/终止 python 脚本运行
- sql - 当列中并非所有值都相等时返回数据
- linux - 如何摆脱在linux中运行的kswapd0进程
- bluetooth-lowenergy - 在 Python BLE 实现中如何调用 StartNofity?
- database-migration - 我正在使用 Flyway 数据库迁移工具。我可以存档已迁移的工件还是必须保留在当前文件夹下?
- php - 如何在 PHP 中使用回调函数
- java - IntelliJ IDEA IDE 中的默认 Ant 配置是什么样的
- mysql - MySQL 使用游标克隆现有表