def playAgain():
b = input('Do you want to play again? y/n')
if b == ('y'):
def startGame():
startGame()
else:
print('Goodbye!')
time.sleep(1)
sys.exit()
import random
import time
import sys
global shots
shots = 0
while shots <=5:
chanceofDeath =random.randint(1,6)
input('press enter to play Russian roulette.')
if chanceofDeath ==1:
shots = shots + 1
print (shots)
print('You died.')
time.sleep(1)
playAgain()
else:
shots = shots + 1
print (shots)
print ('click')
if shots == 5:
print('You won without dying!')
time.sleep(1)
playAgain()
当我运行程序时,当它要求再次播放时,如果你选择是,它可以工作,但从最后一次拍摄开始。例如,如果您在第二次拍摄中死亡并再次播放,而不是重新开始,它将立即从3开始。如何每次重置镜头?
答案 0 :(得分:0)
它继续上一次拍摄的原因是因为你从未真正将'镜头'设置回0代码:
import random
import time
import sys
global shots
shots = 0
只运行一次,这意味着镜头永远不会被分配回0。
你想要的是如果用户选择再次播放,'shots'变量应该设置为0.如果用户想再次播放,你可以编辑你的playAgain()函数返回True。例如:
def playAgain():
b = input('Do you want to play again? y/n')
if b == ('y'):
return True
else:
print('Goodbye!')
time.sleep(1)
sys.exit()
这允许您检查用户是否想要在主while循环中再次播放并将'shots'设置为0,如下所示:
if playAgain():
shots = 0
同样,在任何函数之外声明镜头,而while循环是唯一使用它的东西,它不需要被定义为全局变量。
def playAgain():
b = input('Do you want to play again? y/n')
if b == ('y'):
return True
else:
print('Goodbye!')
time.sleep(1)
sys.exit()
import random
import time
import sys
shots = 0
while shots <=5:
chanceofDeath =random.randint(1,6)
input('press enter to play Russian roulette.')
if chanceofDeath ==1:
shots = shots + 1
print (shots)
print('You died.')
time.sleep(1)
if playAgain():
shots = 0
else:
shots = shots + 1
print (shots)
print ('click')
if shots == 5:
print('You won without dying!')
time.sleep(1)
if playAgain():
shots = 0
此外,我不确定您希望代码如何处理以下内容:
def startGame():
startGame()
希望这有帮助