在python上的胡扯模拟器

时间:2014-12-12 02:29:09

标签: python loops python-3.x while-loop

我正在为这个叫做掷骰子的赌场游戏做一个程序。我花了很多时间做这件事,但我卡住了,我似乎无法找到问题所在。它没有正确循环它并且输出不正确。

这是我到目前为止的代码

import random
from easygui import *
ans = boolbox("Would you like to play Craps?")
while ans == True:
    d1 = random.randint(1,6)
    d2 = random.randint(1,6)
    point = d1+d2
    counter = 1  
    string = ""
    if point == 7 or point == 11:  
        string += ("Roll #{}: [{}] [{}] ==> WIN!".format(counter,d1,d2))
    elif point == 2 or point == 3 or point == 12:
        string += ("Roll #{}: [{}] [{}] ==> LOSS!".format(counter,d1,d2)) 
    else:
        string += ("Roll #{}: [{}] [{}] ==> 'point' is {}".format(counter, d1,d2,point))
        num = point
        while num != 7:
            if counter == 1:
                string += ("Roll #{}: [{}] [{}] ==> keep going!".format(counter, d1,d2))
                counter +=1
                dice_1 = random.randint(1,6)
                dice_2 = random.randint(1,6)
                num = dice_1 + dice_2
                if num == point:
                    string += ("Roll #{}: [{}] [{}] ==> WIN!".format(counter, dice_1,dice_2))
                    counter +=1
                elif num == 7:
                    string+= ("Roll #{}: [{}] [{}] ==> LOSE!".format(counter, dice_1,dice_2))
                    counter+=1
                else:
                    string+= ("Roll #{}: [{}] [{}] ==> keep going!".format(counter,dice_1,dice_2))

1 个答案:

答案 0 :(得分:0)

以上代码是您尝试执行的操作的简化版本。它可以正常工作。特别是,我移动了craps核心功能

import random
from easygui import *

def rollDice(): # simulate a dice roll
    return random.randrange(1,7) + random.randrange(1,7)

def craps(): # roll dice and evaluate results
    firstRoll= rollDice()
    if firstRoll in (7,11):
        return 1 # initial winning condition
    if firstRoll in (2,3,12):
        return 0 #initial losing condition

    while True:
        newRoll = rollDice()
        if newRoll == firstRoll:
            return 1 #secondary winning condition
        if newRoll == 7:
            return 0 #secondary losing condition

ans = boolbox("Would you like to play Craps?")
while ans == True:
    result = craps()
    string = ""
    if( result == 1 ):
        string += ("WIN!")
    else:
        string += ("LOSS!")
    msgbox(string)
    ans = boolbox("Would you like to play Craps?")
编辑:严格分配??? :)

import random
from easygui import *

ans = boolbox("Would you like to play Craps?")
while ans == True:
    looping = True
    firstRoll = random.randrange(1,7) + random.randrange(1,7)
    if firstRoll in (7,11):
        result = 1 # initial winning condition
        looping = False
    if firstRoll in (2,3,12):
        result = 0 #initial losing condition
        looping = False

    while looping:
        newRoll = random.randrange(1,7) + random.randrange(1,7)
        if newRoll == firstRoll:
            result = 1 #secondary winning condition
            looping = False
        if newRoll == 7:
            result = 0 #secondary losing condition
            looping = False

    string = ""
    if( result == 1 ):
        string += ("WIN!")
    else:
        string += ("LOSS!")
    msgbox(string)
    ans = boolbox("Would you like to play Craps?")