我自己写的。
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()
---------------------------------------------------------------------------------------------------------------------------------
题目
总结:函数分工不明确,逻辑不清晰。代码块应分工明确,合理调用。