우리는 파이썬 프로그램을 만들어 크랩 게임을 시뮬레이션하고 승리 확률을 표시하려고합니다. while 루프에 대한 답을 좁히고 제목에서 알 수 있듯이 루프를 치면 종료하지 않습니다. 지금까지 루프가 끝나야한다고 말할 수는 있지만 대신에 "거짓"의 끝없는 열차가 돌아옵니다. 다음은 전체 프로그램에 대한 주석이있는 코드입니다. 맨 아래에는 크랩에 대한 규칙을 입력 해 보겠습니다. while 루프가 파이썬에서 멈추지 않습니다
import random
def craps()
roll = random.randint(1,6) + random.randint(1,6)
if (roll == 7 or roll == 11): #Tests if the player should win. If so it adds a 1 to the win counter
return 1
elif (roll == 2 or roll == 3 or roll == 12): #Tests if player should lose. If so adds 0
return 0
else:
roll2 = 0 #initializes roll2 for the while
while (roll2 != roll or roll2 != 7): #The player keeps rolling until they get a 7 or the initial roll
roll2 == random.randint(1,6) + random.randint(1,6) #Rolls the dice
if (roll2 == roll): #Tests if the player should win
return 1
elif (roll2 == 7): #Tests if the player should lose
return 0
win = 0 #Start the win counter at 0
games = int(input("Enter the amount of games you want played: ")) #Accept how many games will be played
for i in range (games):
win = win + craps() #adds a 1 or a 0 depending on if a game was won
print ("The probability of winning is:", win, "/", games, " =", float(win)/games) #print the probability of winning
우리가 사용하는 기본적인 규칙
은 : 초기 롤 7 또는 11 플레이어 승 (라인 4) 인 경우, 플레이어가 2 여섯 개면 주사위를 굴린다. 플레이어가 2, 3 또는 12를 굴리는 경우 플레이어는 잃습니다 (6 행). 플레이어가 초기 롤 또는 롤에 일치 할 때까지 롤링을 유지합니다. 7. 초기 롤과 일치하면 승리합니다. 그들은 7 점을 잃습니다 (10-15 행). 우리의 프로그램은 선수가이기는 확률을 시뮬레이션하고 표시하기로되어 있습니다.나는이 사이트를 처음 사용하기 때문에 앞으로 어떤 일을 망쳤는지 알려주십시오. 도와 줘서 고마워!
아마도 def 문 (콜론 필요)은 win = 0 행까지 확장됩니까? –