所以,我为我的项目制作了俄罗斯轮盘游戏。我变成了一个功能,现在我想要追踪。我想要前,150次后“枪”就会卡壳。我还想跟踪输出,以便我可以绘制图表,哪个数字出现最频繁等。下面的代码将被更改,以便它可以无限期地运行顺便说一句
myList = ["1", "2", "3", "4", "5", "6"] 随机导入 #洗牌列表,就像在左轮手枪上旋转鼓一样 随机播放(myList) 打印(我的列表)
#随机选择一个号码先拍 拍摄 = random.choice(myList) def RussianRoullete(shoot,myList): 触发拉力 = 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.")
结果=俄罗斯轮盘(射击,myList) 打印(结果)
这是我到目前为止的代码,我真的对此一无所知,因为我对 python 有点陌生,对模型也绝对是新手!
编码花了足够长的时间。花点时间理解代码:
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() 即可。