1. 订立制作目标

做任何程序(生活也是如此)之前,必须做一个规划,就是说你想做什么、你想做到什么。

目标:

  1. 输入两个数字,一个是最低值,一个是最高值
  2. 随机生成一个数字
    重复:
  3. 问他需不需要再生成或者关闭程序
  4. 重复运行第1-3的代码

2. 制作所需函数或模块

根据‘目标’,估算所需函数或者模块

  1. 输入两个数字,一个是最低值,一个是最高值 (变量、input函数、int数据类型)
  2. 随机生成一个数字 (random模块、print函数)
    重复:
  3. 问他需不需要再生成或者关闭程序 (exit函数、if函数、input函数、变量、int数据类型)
  4. 重复运行第1-3的代码 (while函数)

3. 开始制作

1. 第一步(import模块)

由于Python经常把模块放在最前,所以首先要import(载入、输入)函数。

  1. 随机生成一个数字 (random模块、print函数)
  2. 问他需不需要再生成或者关闭程序 (exit函数、if函数、input函数、变量、int数据类型)
  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块

2. 第二步

  1. 输入两个数字,一个是最低值,一个是最高值 (变量、input函数、int数据类型)

我用‘low’代表最低值;‘high’代表最高值
备注:由于复杂程序需要用到很多变量,所以最好变量命名是跟该代码有关,以免混淆

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. low=int(input('输入最低值:'))
  4. high=int(input('输入最高值:'))

3. 第三步

  1. 随机生成一个数字 (random模块、print函数)

random 函数格式

random.randint(最低值,最高值)

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. low=int(input('输入最低值:'))
  4. high=int(input('输入最高值:'))
  5. print(random.randint(low,high))

4. 第四步

  1. 问他需不需要再生成或者关闭程序 (exit函数、if函数、input函数、变量、int数据类型)

用‘ask’代表回答

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. low=int(input('输入最低值:'))
  4. high=int(input('输入最高值:'))
  5. print(random.randint(low,high))
  6. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  7. if ask==1:
  8. print('继续')
  9. elif ask==0:
  10. exit()
  11. else:
  12. print(输入有误,请重新输入。)

对了,如何弄最后那句重新输入?
这就要用到while函数了。

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. low=int(input('输入最低值:'))
  4. high=int(input('输入最高值:'))
  5. print(random.randint(low,high))
  6. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  7. while ask==0 or 1:
  8. if ask==1:
  9. print('继续')
  10. elif ask==0:
  11. exit()
  12. else:
  13. print('输入有误,请重新输入。')

5. 第五步

  1. 重复运行第1-3的代码 (while函数)
  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. while ask==1:
  4. low=int(input('输入最低值:'))
  5. high=int(input('输入最高值:'))
  6. print(random.randint(low,high))
  7. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  8. while ask==0 or 1:
  9. if ask==1:
  10. print('继续')
  11. elif ask==0:
  12. exit()
  13. else:
  14. print('输入有误,请重新输入。')

3. 运行并debug(解决错误)

第一次运行

运行截图:
【Python教程2】手把手用Python教你制作随机数生成程序 - 图1

Bug 错误

Traceback (most recent call last):
File “C:/Users/abc/Documents/Python/随机数生成.py”, line 3, in
while ask==1:
NameError: name ‘ask’ is not defined

最重要的还是最后一句:未定义名称‘Ask’

debug

要在前面新增一句ask=1
完整代码:

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. ask=1
  4. while ask==1:
  5. low=int(input('输入最低值:'))
  6. high=int(input('输入最高值:'))
  7. print(random.randint(low,high))
  8. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  9. while ask==0 or 1:
  10. if ask==1:
  11. print('继续')
  12. elif ask==0:
  13. exit()
  14. else:
  15. print('输入有误,请重新输入。')

第二次运行

【Python教程2】手把手用Python教你制作随机数生成程序 - 图2

bug

看上图,可以见到再不停运行,如何解决?

debug

我们可以用break放在print('继续')后面。

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. ask=1
  4. while ask==1:
  5. low=int(input('输入最低值:'))
  6. high=int(input('输入最高值:'))
  7. print(random.randint(low,high))
  8. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  9. while ask==0 or 1:
  10. if ask==1:
  11. print('继续')
  12. break
  13. elif ask==0:
  14. exit()
  15. else:
  16. print('输入有误,请重新输入。')

第三次运行

【Python教程2】手把手用Python教你制作随机数生成程序 - 图3

bug

问题如上,不多说,都是加break

debug

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. ask=1
  4. while ask==1:
  5. low=int(input('输入最低值:'))
  6. high=int(input('输入最高值:'))
  7. print(random.randint(low,high))
  8. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  9. while ask==0 or 1:
  10. if ask==1:
  11. print('继续')
  12. break
  13. elif ask==0:
  14. exit()
  15. else:
  16. print('输入有误,请重新输入。')
  17. break

第四次运行

【Python教程2】手把手用Python教你制作随机数生成程序 - 图4

Bug

输入错误后,不能重新输入

  • 原因:加了break

debug

  1. import random #由于random模块系统自带,不需要pip安装模块
  2. from sys import exit #同上,意思为:从系统载入模块
  3. ask=1
  4. while ask==1:
  5. low=int(input('输入最低值:'))
  6. high=int(input('输入最高值:'))
  7. print(random.randint(low,high))
  8. ask=int(input('输入1代表继续,0代表结束程序,请输入:'))
  9. while ask==0 or 1:
  10. if ask==1:
  11. print('继续')
  12. break
  13. elif ask==0:
  14. exit()
  15. else:
  16. print('输入有误,请重新输入。')
  17. ask=1

P.S. 这个方法不算最好,但是实在想不出更好的方法,欢迎大佬在评论区指导。


第五次运行

【Python教程2】手把手用Python教你制作随机数生成程序 - 图5
程序尚算成功。


后话

希望这个教程可以让新手慢慢学会制作一个程序。
源代码我已经放到了gitee(链接:https://gitee.com/alex27933/mypython/tree/master/%E9%9A%8F%E6%9C%BA%E6%95%B0%E7%94%9F%E6%88%90%E7%A8%8B%E5%BA%8F