【发布时间】:2014-05-30 10:38:52
【问题描述】:
所以,我正在研究一个掷骰子程序模拟器,通过浏览网络,并从这个论坛上的用户那里获得一些帮助。我能够完成大部分工作。至少,我认为我的游戏逻辑是正确的。
上次我使用它时它正在运行,但我一定是在没有意识到的情况下更改了一些东西,或者因为现在 while 循环一直在运行。在程序中,骰子在没有用户干预的情况下永远滚动。
有人可以看看代码是否有问题?我已经为此工作了好几个小时,却一无所获。
我再也不赌博了! :/
from Dice import PairOfDice
print("Now Let's play with two dice!")
#
def MainDouble():
bdice = PairOfDice()
doubleDiceRoll = ''
global myPoint #declare global variable
input("Press Enter to Roll the Dice once")
while doubleDiceRoll == '': #Roll the dice (both die objects)
bdice.toss()
print ('The first die reads.. ' + str(bdice.getValue1()) + '\n')
print ('The second die reads.. ' + str(bdice.getValue2()) + '\n')
Val = bdice.getTotal()
##Beginning of conditional blocks##
if Val == 7 or Val == 11:
gamestatus = "WON"
elif Val == 2 or Val == 3 or Val == 12:
gamestatus = "LOST"
if Val != 7 and bdice.getTotal() != 11 and Val != 2 and Val != 3 and Val != 12:
gamestatus = "CONTINUE"
#
myPoint = Val
print("The Point is now " + myPoint + "/n") #display the user's point value
global pSum
pSum = 0
#The point
while gamestatus == "CONTINUE": #Checking the point
global point
point = myPoint
pSum = MainDouble()
if pSum == myPoint:
gamestatus == "WON"
elif pSum == 7:
gamestatus = "LOST"
if gamestatus == "WON":
print("Winner!")
else:
print("Sorry, Seven out!")
print ("Roll Again?")
doubleDiceRoll = input("Roll Again?")
MainDouble()
【问题讨论】:
-
在你的脚本末尾有
doubleDiceRoll = input("Roll Again?"),但它不在while doubleDiceRoll == ''中。没有什么能改变条件,它始终为真
标签: python loops while-loop forever