Python小游戏:猜大小

简介: Python小游戏:猜大小

我自己写的。

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:'))

老师写的。

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()

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

题目

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

 

目录
相关文章
|
5月前
|
人工智能 机器人 测试技术
【python】python小游戏——开心消消乐(源码)【独一无二】
【python】python小游戏——开心消消乐(源码)【独一无二】
|
5月前
|
Python
python小游戏4
python小游戏4
|
5月前
|
Python
python小游戏7
python小游戏7
|
5月前
|
Python
python小游戏6
python小游戏6
|
5月前
|
Python
python小游戏5
python小游戏5
|
5月前
|
Python
python小游戏1
python小游戏1
|
5月前
|
安全 C++ Python
小游戏实战-Python实现石头剪刀布+扫雷小游戏
小游戏实战-Python实现石头剪刀布+扫雷小游戏
79 0
|
5月前
|
数据可视化 安全 数据安全/隐私保护
使用Python做个可视化的“剪刀石头布”小游戏
使用Python做个可视化的“剪刀石头布”小游戏
92 0
|
5月前
|
Python
python小游戏3
python小游戏3
|
5月前
|
Python
python小游戏
python小游戏
下一篇
无影云桌面