欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

用python写一个掷骰子的游戏,并统计

程序员文章站 2022-07-07 10:38:45
...
"""
库函数调用
"""
import random


"""
内部函数
"""
#生成随机数
def dice_pts():
    return random.randint(1,6);

#玩一局掷骰子
def roll_dice():
    a = dice_pts();
    b = dice_pts();
    if a>b:
        print ('Player A has won: ',a,'pts','\n',
                'Player B has won:',b,'pts','\n',
               'A wins!! \n');
        return 1
    elif b>a:
        print ('Player A has won: ',a,'pts','\n',
                'Player B has won:',b,'pts','\n',
               'B wins!! \n');
        return 2
    else:
         print("no winner please play again");
         return 0
        
#玩任意局数掷骰子,统计结果
def play_game():
    print('please input game times of course a positive integer \n');  
    num = int(input()); 
    count = 1;
    WinCountForA = 0;
    WinCountForB = 0;
    drew = 0;
    
    while count <= num:
        winner = roll_dice();
        if  winner == 1:
            WinCountForA += 1;
        elif winner == 2:
            WinCountForB += 1;
        elif winner == 0 :    
            drew += 1;
        
        count += 1;
    
    print('Player A has won:', WinCountForA, 'times', '\n',
          'Player B has won:', WinCountForB, 'times', '\n');
    
    if WinCountForA>WinCountForB:
        print ('A wins!!');
    elif WinCountForB>WinCountForA:
        print ('B wins!!');
    else:
         print("no winner");

        
        
        
"""
主函数
"""   
if __name__ == '__main__':
    play_game();
相关标签: python课堂实验