【python学习1】编写猜数字的小游戏

2021/12/6 17:16:53

本文主要是介绍【python学习1】编写猜数字的小游戏,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

跟着老师的课学习python哈哈~

#生成随机数
# 模块moudule === 工具箱
#  导入random
from time import sleep 

import random
#生成随机数
n = random.randint(0,1000)

while True:
     m = int(input('please input a number\n'))
#m为获取的用户输入数字,通过比较大小,给予用户提示,从而继续游戏
     if m > n:
        print('too big')
     elif m < n:
        print('too small')
     else:
#猜到正确数字后,睡眠10秒,再自动退出程序
        print('you win')
        sleep(10)
        break 

另外,我们把这个小游戏导出成可运行的exe程序

1、先下载 pyinstaller的安装文件,下载地址:http://www.pyinstaller.org/downloads.html

2、直接下载:pyinstaller 3.4

3、下载到本地后,直接解压,然后通过管理员模式打开命令窗口,用 cd 命令切换至 pyinstaller的解压路径,然后运行 python setup.py install

4、安装的过程会花点时间,就耐心等待吧,出现下面的提示表明安装成功

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

Best match: setuptools 40.8.0

Adding setuptools  40.8 . 0  to easy - install.pth  file Installing easy_install - script.py script to C:\Program Files\Python3\Scripts Installing easy_install.exe script to C:\Program Files\Python3\Scripts Installing easy_install - 3.6 - script.py script to C:\Program Files\Python3\Scripts Installing easy_install - 3.6 .exe script to C:\Program Files\Python3\Scripts Using c:\program files\python3\lib\site - packages Finished processing dependencies  for  PyInstaller = = 3.4 ---------------------------------------------------------------------------------------------------------------------   5、通过 pip show pyinstaller查看详细信息进一步确认安装情况

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

PS C:\Windows\system32> pip show pyinstaller

Name: pyinstaller Version:  3.4 Summary: PyInstaller bundles a Python application  and  all  its dependencies into a single package. Home - page: http: / / www.pyinstaller.org Author: Giovanni Bajo, Hartmut Goebel, David Vierra, David Cortesi, Martin Zibricky Author - email: pyinstaller@googlegroups.com License: GPL license with a special exception which allows to use PyInstaller to build  and  distribute non - free programs (including commercial ones) Location: c:\program files\python3\lib\site - packages\pyinstaller - 3.4 - py3. 6.egg Requires: setuptools, pefile, macholib, altgraph, pywin32 - ctypes Required - by: --------------------------------------------------------------------------------------------------------------------- 6、用命令“pip install pywin32”安装Pywin32,或者下载一个pywin32来安装,注意文件对应的Python版本。

7、cmd(或Power shell)进入py程序项目目录,执行命令:pyinstaller -F -w --icon=xxx.ico main.py --noconsole。其中,-F表示生成单exe可执行文件,-w表示窗体程序,
--icon是设置exe的显示图标,'main.py'是程序的入口,--noconsole 表示不展示cmd窗口,反过来想看cmd窗口就改成--console。

编译:pyinstaller -F -w game.py  (-F表示打包单个文件,-w是为了打开exe时候不弹出黑框)

设置exe的图标:pyinstaller -F -w -i bitbug_favicon.ico game.py  (-i用来设置编译成exe文件的图标,后面跟.ico格式的图片文件)

我这一步这样执行生成的exe执行是报错的,报错内容

Failed to execute script

经过百度以后,直接pyinstaller -F game.py ,不加-w,生成的小程序可以成功运行

在dist目录下有个可执行的exe程序

 

 

 

 这样就可以开发出一个解闷的小游戏了~哈哈

 



这篇关于【python学习1】编写猜数字的小游戏的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程