所以我写了一个应该模拟Pig游戏的程序,人们掷骰子直到100分。代码很好,直到它从播放器切换到计算机,计算机只是将它放入一个没有变化的循环中,我无法弄清楚它有什么问题。任何帮助表示赞赏!
import random
def comp_roll():
CompScore = 0
die1 = random.randint (1, 6)
die2 = random.randint (1, 6)
BothDice = die1 + die2
print("Rolled {} and {}" .format(die1, die2))
print("Computer earned", BothDice, "points.")
def comp():
CompScore = 0
die1 = random.randint (1, 6)
die2 = random.randint (1, 6)
BothDice = die1 + die2
while CompScore < 100 :
if die1 == 1 and die2 != 1 :
print("Rolled {} and {}" .format(die1, die2))
print ("Computer's turn is over!")
input("Please click enter to continue...")
dice()
elif die1 != 1 and die2 ==1 :
print("Rolled {} and {}" .format(die1, die2))
print ("Computer's turn is over!")
input("Please click enter to continue...")
dice()
elif die1 != 1 and die2 != 1:
CompScore = CompScore + BothDice
print("Rolled {} and {}" .format(die1, die2))
print("Computer earned", BothDice, "points!")
input("Please click enter to continue...")
elif die1 ==1 and die2 ==1 :
CompScore = PlayerScore + 25
print("Computer rolled double 1 and earned 25 points.")
print("Computer score is now ", CompScore)
input("Please click enter to continue...")
if CompScore >= 100:
print("You lose!")
input("click enter to exit program.")
exit()
def dice():
PlayerScore = 0
CompScore = 0
die1 = random.randint (1, 6)
die2 = random.randint (1, 6)
BothDice = die1 + die2
print("you rolled a", die1, "and", die2)
while PlayerScore <100 :
if die1 == 1 and die2 != 1 :
print("you rolled a", die1, "and", die2)
print ("your turn is over!")
input("Please click enter to continue...")
comp()
elif die1 != 1 and die2 ==1 :
print("you rolled a", die1, "and", die2)
print ("your turn is over!")
input("Please click enter to continue...")
comp()
elif die1 != 1 and die2 != 1:
PlayerScore = PlayerScore +BothDice
print("You earned", BothDice, "points!")
print ("Your total is now ", PlayerScore)
reply = str(input("Would you like to roll again? 'y' or 'n'"))
if reply == 'y' :
die1 = random.randint (1, 6)
die2 = random.randint (1, 6)
BothDice = die1 + die2
print("you rolled a", die1, "and", die2)
elif reply == 'n':
while CompScore < 100 :
if die1 == 1 and die2 != 1 :
print("Rolled {} and {}" .format(die1, die2))
print ("Computer's turn is over!")
input("Please click enter to continue...")
return
elif die1 != 1 and die2 ==1 :
print("Rolled {} and {}" .format(die1, die2))
print ("Computer's turn is over!")
input("Please click enter to continue...")
return
elif die1 != 1 and die2 != 1:
CompScore = CompScore + BothDice
print("Computer earned", BothDice, "points!")
input("Please click enter to continue...")
return
elif die1 ==1 and die2 ==1 :
CompScore = PlayerScore + 25
print("Computer rolled double 1 and earned 25 points.")
print("Computer score is now ", CompScore)
input("Please click enter to continue...")
return
if CompScore >= 100:
print("You lose!")
input("click enter to exit program.")
exit()
elif die1 ==1 and die2 ==1 :
PlayerScore = PlayerScore + 25
print("You rolled double 1 and earned 25 points.")
print("Your total score is now ", PlayerScore)
input("Please click enter to continue...")
roll()
if PlayerScore == 100 :
print("You win!")
break
elif CompScore == 100:
print("You lose!")
break
dice()