核心知识&大纲:

image.png

课程目录:
课程介绍
一次猜测
支持多次猜测-while循环
限制猜测次数-for循环
通过argv指定猜测次数
记录猜测时间
支持多轮游戏
用tuple记录每轮的数据
显示每轮的数据
用lambda显示最快记录和最慢记录
for循环详解
基于列表的循环
基于range的循环
循环继续
循环退出
毕套循环
一次性退出嵌套循环
显示循环的index和值
到底可以基于什么循环
实现一个自己的可循环类
命名规范初步
重构出函数
猜数字游戏第二级-多节
函数详解-多节
错误和异常详解-多节

猜测程序

while

  1. import random
  2. answer = random.randint(1,10)
  3. guess_limit = 3#最大猜测次数
  4. guess_count = 0#当前猜测次数
  5. while(guess_count < guess_limit):
  6. guess = eval(input('1-10之间数字,猜猜?'))
  7. if(answer == guess):
  8. print('guess Right!')
  9. print('game over! 88')
  10. break;
  11. elif(guess > answer):
  12. print('Too Big!',end='')
  13. else:
  14. print('Too Small!',end='')
  15. guess_count += 1
  16. if(guess_limit == guess_count):
  17. print('你已经用完猜测次数:' + str(guess_limit))
  18. print('You Have Failed!')
  19. break
  20. else:
  21. print('Guess Please!')

for

  1. import random
  2. answer = random.randint(1,10)
  3. guess_limit = 3#最大猜测次数
  4. is_right = False
  5. for i in range(3):
  6. guess = eval(input('1-10之间数字,猜猜?'))
  7. if(answer == guess):
  8. is_right = True
  9. break
  10. elif(guess > answer):
  11. print('Too Big!',end='')
  12. else:
  13. print('Too Small!',end='')
  14. if(i < guess_limit-1):
  15. print('Guess Please!')
  16. if(is_right):
  17. print(f'You Have Win!The answer is {answer}')
  18. else:
  19. print('你已经用完猜测次数:' + str(guess_limit))
  20. print('You Have Failed!')

第一阶段代码

  1. #支持输入猜测次数
  2. #支持时间记录 used_time = end_time - start_time
  3. #支持多轮游戏 while if break
  4. #tuple 元组 记录每局数据 .append((cycle,is_right,used_time))
  5. #三元运算替代 label = 'WIN' if(_is_right) else 'FAIL'
  6. #lambda best_score = min(score,key=lambda x:x[2])
  7. #lambda best_score = min(score,key=lambda x:x[2] if x[1] else 9999)
  8. #try except 异常处理-有异常即捕获
  9. #异常处理 对症下药
  10. import random
  11. import sys
  12. import time
  13. try:
  14. guess_limit = int(sys.argv[1])#指定最大猜测次数
  15. #print(a)
  16. except ValueError:#异常处理模块
  17. guess_limit = 4 #默认最大输入次数4
  18. print('输入次数有错,使用默认值:4')
  19. except NameError:
  20. print('name error ignore')
  21. score = []#分数
  22. cycle = 0#第几轮
  23. while(True):
  24. cycle += 1
  25. begin_time = time.time()
  26. answer = random.randint(1,10)
  27. is_right = False
  28. for i in range(guess_limit):
  29. try:
  30. guess = eval(input('1-10之间数字,猜猜?'))
  31. except NameError:
  32. print('别乱说,输入1-10之间的数字')
  33. continue
  34. if(answer == guess):
  35. is_right = True
  36. break
  37. elif(guess > answer):
  38. print('Too Big!',end='')
  39. else:
  40. print('Too Small!',end='')
  41. if(i < guess_limit-1):
  42. print('Guess Please!')
  43. if(is_right):
  44. print(f'You Have Win!The answer is {answer}')
  45. else:
  46. print('你已经用完猜测次数:' + str(guess_limit))
  47. print('You Have Failed!')
  48. end_time = time.time()
  49. used_time = round((end_time - begin_time),2)
  50. print(f'共用时{used_time}')
  51. #保存战绩
  52. score.append((cycle,is_right,used_time))
  53. best_score = min(score,key=lambda x:x[2] if x[1] else 9999)
  54. con = input('是否继续,按q退出')
  55. if (con == 'q'):
  56. print('============战绩============')
  57. for _cycle,_is_right,_used_time in score:
  58. best_label = ' best' if(_cycle == best_score[0] and best_score[1]) else ''#设定最好的标记
  59. label = 'WIN' if(_is_right) else 'FAIL'
  60. print(f'{_cycle}轮,{label},{_used_time}{best_label}')
  61. print('============战绩============')
  62. break