Python入门综合试题:猜大小

游戏规则

游戏开始,首先玩家选择押大小,选择完成后开始摇三个骰子计算总值,总值大于11小于18位大,总值大于3小于10位小,然后告诉玩家猜对或猜错的结果。

程序必要知识

a_list=[1,2,3]
print(sum(a_list))

使用sum()函数对列表中的所有整数求和

import random
point1=random.randrange(1,7)
point2=random.randrange(1,7)
point3=random.randrange(1,7)
print(point1,point2,point3)

导入random内置库然后使用它生成随机数

设计思路

首先需要构建一个摇骰子的函数。1.需要摇3个骰子,每个骰子都生成1-6随机数。2.创建一个列表把摇骰子的结果储存在列表里面,并且每局游戏都更改结果。3.要把点数转换为大或小。4.最后,让用户猜大小,并告诉最后结果。

程序编写

import random
#骰子函数
def Dice(num=3,point=None):
    if point is None:
       points=[]
    while num>0:
       point=random.randrange(1,7)
       points.append(point)
       num-=1
    return points
#根据骰子比大小
def roll_result(total):
    isBig=11<=total<=18
    isSmall=3<=total<=11
    if isBig:
       return 'Big'
    if isSmall:
       return'Small'
def start_game():
    choices=['Big','Small']
    your_choices=input('Big or Small')
    if your_choices in choices:
       point=Dice()
       total=sum(Dice())
       YouWin=your_choices==roll_result(total)
       if YouWin:
          print('The point are',point,'You Win')
       else:
          print('The point are',point,'You Lose')
    else:
          print('Invalid Words')
          start_game()
start_game()
         

猜你喜欢

转载自blog.csdn.net/weixin_42289215/article/details/82834395