| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 917 人关注过本帖
标题:python自己逻辑混乱
只看楼主 加入收藏
tommmm3
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2011-3-22
结帖率:0
收藏
已结贴  问题点数:0 回复次数:6 
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')

搜索更多相关主题的帖子: 继续游戏 
2011-03-22 20:10
libralibra
Rank: 2
等 级:论坛游民
帖 子:8
专家分:19
注 册:2010-8-13
收藏
得分:10 
楼主加我q,790404545
代码写完了,测试如下:

程序代码:
IDLE 2.6.6      ==== No Subprocess ====
>>>
Would you like to play(Y/N):m                               # 测试错误输入
Wrong input!
Would you like to play(Y/N):r                                 # 测试错误输入
Wrong input!
Would you like to play(Y/N):n                                # 测试n
>>>
Would you like to play(Y/N):y                                # 开始测试游戏流程
Great! What's you name:test
Hi, test!
===========================Player START===========================
|--Dic1: 4; Dic2: 6
|---No ONEs! Score for this round: 10; Total score: 10
Do you want roll or hold?(r/h)h                            # 测试一次h,以后全部选择r
===========================Player END===========================
===========================Computer START===========================
|--Dic1: 1; Dic2: 2
|---There's 1 ONE, round is ended!
===========================Computer END===========================
===========================Player START===========================
|--Dic1: 2; Dic2: 3
|---No ONEs! Score for this round: 5; Total score: 15
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 1; Dic2: 2
|---There's 1 ONE, round is ended!
===========================Player END===========================
===========================Computer START===========================
|--Dic1: 5; Dic2: 1
|---There's 1 ONE, round is ended!
===========================Computer END===========================
===========================Player START===========================
|--Dic1: 2; Dic2: 6
|---No ONEs! Score for this round: 8; Total score: 23
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 6; Dic2: 3
|---No ONEs! Score for this round: 9; Total score: 32
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 2; Dic2: 6
|---No ONEs! Score for this round: 8; Total score: 40
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 3; Dic2: 4
|---No ONEs! Score for this round: 7; Total score: 47
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 6; Dic2: 6
|---No ONEs! Score for this round: 12; Total score: 59
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 3; Dic2: 5
|---No ONEs! Score for this round: 8; Total score: 67
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 4; Dic2: 3
|---No ONEs! Score for this round: 7; Total score: 74
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 1; Dic2: 4
|---There's 1 ONE, round is ended!
===========================Player END===========================
===========================Computer START===========================
|--Dic1: 1; Dic2: 2
|---There's 1 ONE, round is ended!
===========================Computer END===========================
===========================Player START===========================
|--Dic1: 4; Dic2: 2
|---No ONEs! Score for this round: 6; Total score: 80
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 3; Dic2: 5
|---No ONEs! Score for this round: 8; Total score: 88
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 6; Dic2: 3
|---No ONEs! Score for this round: 9; Total score: 97
Do you want roll or hold?(r/h)r
===========================Player START===========================
|--Dic1: 3; Dic2: 2
|---No ONEs! Score for this round: 5; Total score: 102        # 达到100分,胜利
*************Congratulations! You win!*************
Would you like to play(Y/N):n                                               # 询问是否重新开始
>>>



[ 本帖最后由 libralibra 于 2011-3-26 04:26 编辑 ]

接VB,MATLAB,python程序
QQ: 790404545
2011-03-26 04:16
yangfanconan
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:10
帖 子:397
专家分:541
注 册:2009-9-22
收藏
得分:10 
关注一下。
2011-03-27 16:59
→_→破烂哥
Rank: 1
来 自:陕西 渭南
等 级:新手上路
帖 子:7
专家分:0
注 册:2011-4-1
收藏
得分:0 

→_→破烂哥→_→
2011-04-01 11:06
qingduo04
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2010-6-11
收藏
得分:0 
二楼,能把最终代码贴出来么?
2011-08-15 23:02
szlmwbnmc
Rank: 1
等 级:新手上路
帖 子:7
专家分:3
注 册:2021-10-9
收藏
得分:0 
2021-10-09 11:42
Bc_Newboy
Rank: 2
等 级:论坛游民
帖 子:76
专家分:99
注 册:2019-4-14
收藏
得分:0 
非常棒
2021-10-09 14:32
快速回复:python自己逻辑混乱
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025826 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved