我为我的项目制作了这个俄罗斯轮盘游戏。我把它变成了一个函数,现在我想跟踪。我想举个例子,150次后“枪”就会卡壳。我还想跟踪输出,以便我可以绘制图表,哪个数字出现最频繁等。
myList = ["1", "2", "3", "4", "5", "6"]
import random
#shuffliing the list, kinda like spinning the drum thing on a revolver
random.shuffle(myList)
print(myList)
#randomly selecting a number to shoot first
shoot = random.choice(myList)
def russianRoullete(shoot,myList):
trigPull = 0
while trigPull < 6:
input("Press enter to pull the trigger")
#code below makes sure a chamber thats been "shot" wont be picked again!
shoot1=random.randrange(len(myList))
shoot2 = myList.pop(shoot1)
#program runs this and it "shoots" the gun
print("You pulled the trigger on chamber", shoot2)
trigPull+=1
if shoot2 == "6":
print("BANG! Game over.")
break
else:
print("Click... You survived this time.")
result = russianRoullete(shoot, myList)
print(result)
编码花了足够长的时间。花点时间理解代码:
import random
game_count = 0
outcome_tracker = []
def russian_roulette():
global game_count, outcome_tracker
if game_count >= 150:
print("The gun is jammed and it needs to be repaired! 😬")
return "Jammed"
myList = ["1", "2", "3", "4", "5", "6"]
random.shuffle(myList)
print("Chambers shuffled:", myList)
trigPull = 0
while trigPull < 6:
input("Press enter to pull the trigger 🤠")
if len(myList) == 0:
print("All chambers are empty. Reloading... 😒")
break
shoot_index = random.randrange(len(myList))
chamber_shot = myList.pop(shoot_index)
print("You pulled the trigger on chamber", chamber_shot)
trigPull += 1
if chamber_shot == "6":
print("BANG! Game over. 🤯")
outcome_tracker.append((game_count + 1, int(chamber_shot)))
break
else:
print("Click... You survived... for now at least. 😬")
game_count += 1
return chamber_shot
result = russian_roulette()
print("Result of this game:", result)
要跟踪多个游戏的结果,只需循环运行 Russian_roulette() 即可。