python自己逻辑混乱
编程初学者,用while语句就开始逻辑混乱,自己做作业,已经发晕,每次调试都无法改正,求指导规则是这样的:玩家和电脑,各两颗骰子,如果掷出的两颗骰子没有1,则分数累积,玩家可选择是否继续或结束回合,如果两颗骰子之中有一个1,则所得分数不予累计,强制结束回合,如果掷到两个1,累计的分数全部清零,并且强制结束回合,由电脑继续游戏。如此,直到有一方累计分数达到100或以上的,游戏胜利。题目中给予电脑的条件是只要在一回合中累计得分20点,则结束回合,由玩家继续游戏。
程序代码:
# Start # Play control: play = input('Would you like to play PIG (y/n)? ')# Play control while play == 'y': # Welcome: username = input('Great! What is your name? ') print('Hi',username) # Game: scoreP = 0# Player's potential score scoreC = 0# Computer's potential score turn = 0# Turn control(Player: 0, Computer:1) # Player's turn: print('|------------------- Start Player\'s Turn ---------------------') # Roll or hold: while turn == 0: hold = input('| Would you like to roll or hold (r/h)? ') while hold =='r': totalP = 0# Player's turn total import random dicP1 = random.randint(1,6)# Player's dice 1 dicP2 = random.randint(1,6)# Player's dice 2 toP = 0# Total of this turn # No 1 if dicP1 and dicP2 != 1: stepP = dicP1 + dicP2# Step total toP = totalP + stepP scoreP = scoreP + toP print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP) print('| Turn total:',toP,'| Potential score:',scoreP) print('|') hold = input('| Would you like to roll or hold (r/h)? ') # If end # Single 1 elif dicP1 or dicP2 == 1: stepP = dicP1 + dicP2 print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP) print('|') print('| *** You rolled a ONE - Turn over - No points for you!') # Turn end show: print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC) print('|------------------- End Player\'s Turn -----------------------') print('|------------------- Start Computer\'s Turn -------------------') turn = turn + 1 # Elif end # Double 1s else: stepP = dicP1 + dicP2 totalP = 0 print('| Die 1:',dicP1,'| Die 2:',dicP2,'| Die total:',stepP) print('|') print('| *** Ouch! That hurts! Two ONEs - You lose your entire score!') print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC) print('|------------------- End Player\'s Turn -----------------------') print('|------------------- Start Computer\'s Turn -------------------') turn = turn + 1 # Else end # While End else: turn = turn+ 1 print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC) print('|------------------- End Player\'s Turn -----------------------') print('|------------------- Start Computer\'s Turn -------------------') # while end # Else end # While end # Player's Turn end # Computer's turn: else: # Roll or hold: totalC = 0 while totalC < 20: import random dicC1 = random.randint(1,6)# Computer's dice 1 dicC2 = random.randint(1,6)# Computer's dice 2 # No 1 if dicC1 and dicC2 != 1: stepC = dicC1 + dicC2# Step total toC = stepC + totalC scoreC = scoreC + toC print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC) print('| Turn total:',toC,'| Potential score:',scoreC) print('|') # If end # Single 1 elif dicC1 or dicC2 == 1: stepC = dicC1 + dicC2 print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC) print('|') print('| *** Computer rolled a ONE - Turn over - No points for computer!') # Turn end show: print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC) print('|------------------- End Computer\'s Turn ---------------------') turn = turn - 1 # Elif end # Double 1s else: stepC = dicC1 + dicC2 totalC = 0 print('| Die 1:',dicC1,'| Die 2:',dicC2,'| Die total:',stepC) print('|') print('| *** Ouch! That hurts! Two ONEs - Computer lose entire score!') print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC) print('|------------------- End Computer\'s Turn ---------------------') turn = turn - 1 # Else end # While end else: print('| *** Kramer\'s score:',scoreP,'| Computer\'s score:',scoreC) print('|------------------- End Computer\'s Turn ---------------------') turn = turn - 1 # Else end # Computer's turn end # Replay control: play = input('Would you like to play again (y/n)? ') # While end # else: print('Thanks for playing')