基于python的垃圾模拟器
我正在为这个赌场游戏做一个叫做“垃圾”的程序。我花了很多时间做这件事,但我被卡住了,似乎找不到问题所在。它不能正确地循环,也不能正确地输出 这是到目前为止我的代码基于python的垃圾模拟器,python,loops,python-3.x,while-loop,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) poin
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))
以上代码是您尝试执行的操作的简化版本。。它是有效的。特别是,我移动了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?")
编辑:严格赋值??:)
它应该模拟垃圾赌场游戏,你认为问题出在哪里?它现在到底在做什么?请看,我认为问题是在我将num的值更改为point之后,while循环的参数不合适。现在它正在做一堆奇怪的事情,它不应该像无限循环那样在一点上不输出任何东西对不起,但是如果使用while/for循环,如果使用/elif/else,它是否可能呢?哈哈,这不是一个严格的赋值,更像是我们还没有学会这些:P当我运行编辑过的代码时,它会进入一个无限循环?@shahaadn:No,如果你对boolbox问题回答“否”,那么,
ans
变为False
,while条件变为False
,因此应用程序退出。如果我的回答帮助你解决了问题,记得接受我的回答。
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?")