首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Python小游戏:猜大小

Python小游戏:猜大小

作者头像
YueXuan
发布2025-08-18 22:32:48
发布2025-08-18 22:32:48
12700
代码可运行
举报
运行总次数:0
代码可运行

我自己写的。

代码语言:javascript
代码运行次数:0
运行
复制
import random

def Big_or_Small(choice):
    lst = []
    point1 = random.randrange(1,7)
    point2 = random.randrange(1,7)
    point3 = random.randrange(1,7)
    lst.append(point1)
    lst.append(point2)
    lst.append(point3)
    num = sum(lst)
    print('<<<<< ROLE THE DICE! >>>>>')
    if choice == 'Big':
        if 11 <= num <= 18:
            print('The points are {} You Win!!!'.format(lst))
        else:
            print('The points are {} You Lose!'.format(lst))
    elif choice == 'Small':
        if 11 <= num <= 18:
            print('The points are {} You Win!!!'.format(lst))
        else:
            print('The points are {} You Lose!'.format(lst))
    else:
        print('您输入的格式有误,请重新输入!!!')
        


while True:
    print('-----------------------------------------------')
    print('<<<<< GAME STARTS! >>>>>')
    Big_or_Small(input('Big or Small:'))

老师写的。

代码语言:javascript
代码运行次数:0
运行
复制
import random
def roll_dice(numbers = 3, points = None):
    print('<<<<< GAME STARTS! >>>>>')
    if points is None:
        points = []
    while numbers > 0:
        point = random.randrange(1,7)
        points.append(point)
        numbers = numbers - 1
    return points

def roll_result(total):
    isBig = 11 <= total <= 18
    isSmall = 11 <= total <= 18
    if isBig:
        return 'Big'
    elif isSmall:
        return 'Small'

def start_game():
    print('<<<<< GAME STARTS! >>>>>')
    choices = ['Big','Small']
    your_choice = input('Big or Small:')
    if your_choice in choices:
        points = roll_dice()
        total = sum(points)
        youwin = your_choice ==roll_result(total)
        if youwin:
            print('The points are {} You Win!!!'.format(points))
        else:
            print('The points are {} You Lose!'.format(points))        
    else:
        print('Invalid Words')
        start_game()
        
start_game()

---------------------------------------------------------------------------------------------------------------------------------

题目

总结:函数分工不明确,逻辑不清晰。代码块应分工明确,合理调用。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档