# 20234219 2023-2024-12 《Python程序设计》实验1报告
课程:《Python程序设计》
班级: 2342班
姓名: 罗国淦
学号:20234219
实验教师:王志强
实验日期:2024年3月12日
必修/选修: 公选课
## 1.实验内容
-
1.熟悉Python开发环境;
2.练习Python运行、调试技能;
3.编写程序,练习变量和类型、字符串、对象、缩进和注释等;
4.掌握git技能(可把猜数字游戏上传到gitee)
## 2. 实验过程及结果
-
熟悉Pycharm等开发环境;
-
1.首先在Pycharm的官网下载软件并注册账号。登录账号后申请免费使用权限,即可使用Pycharm;
- 2.Pycharm可以在“设置-插件”中选择各类插件下载,例如中文插件,以增加相关功能,提升使用体验;
- 3.新建项目,再新建文件,命名后即可开始写代码;
-
掌握基本的Python运行和调试技能;
-
1.写好代码后,点击右上角的三角形按钮即可开始运行程序;
-
2.若点击右上角调试按钮,即可对程序进行调试。这个功能可以帮助我们找BUG;
-
掌握基本的Python编程技能。
-
了解Python的变量、结构、语法 并将其运用于程序。此处直接以我编的“十一点半”游戏为例,上代码:
-
import random import sys import time import os #pyinstaller -F .venv/十一点半.py player1=0 computer1=0 list1=[] listpl=[] listco=[] st=3 winner=3 pl=1 cl=1 pll=1 instruct=1 def instruction(): global instruct print(" 本游戏仅需要键盘按键即可玩耍") print("规则:") print(" 1.首先通过“石头剪刀布”游戏决出优先摸牌方,仅需要输入相应的选项字母然后按回车键即可") print(" 2.当“石头剪刀布”游戏胜者为“十一点半”优先摸牌方。摸牌共分五轮,双方轮流摸牌。") print(" 3.每次摸牌,双方可选择摸牌或不摸牌。当选择不摸牌,则以后几轮均不可再摸牌") print(" 4.摸到1——10扑克牌即算作相应的1——10点,“J”、“Q”、“K”扑克牌各算作0.5点") print(" 5.当手中扑克牌总点数小于11.5点时,游戏可进行。当一方扑克牌大于11.5点时,该玩家输掉游戏。当一方扑克牌等于11.5点时,该玩家赢得游戏") print(" 6.当五轮摸牌结束,仍然未分出胜负,则对比自己的总点数,总点数大者获胜") print(" 7.该游戏目前只支持1名人类玩家和1个电脑玩家对战,如有多人对战需求,可联系作者升级游戏") print("按任意键进入游戏") a=input() instruct=0 def stone(): global st while 1: time.sleep(1) f_handler = open('out.log', 'w') oldstdout = sys.stdout sys.stdout = f_handler os.system('cls') sys.stdout = oldstdout print("a.石头、b.剪刀、c.布,你选择:") a=input() b=random.randint(1,3) if a=="a" and b==1: print("平局") continue elif a=="a" and b==2: print("你赢了") st=1 break elif a=="a" and b==3: print("你输了") st=0 break elif a=="b" and b==1: print("你输了") st=0 break elif a=="b" and b==2: print("平局") continue elif a=="b" and b==3: print("你赢了") st = 1 break elif a=="c" and b==1: print("你赢了") st = 1 break elif a=="c" and b==2: print("你输了") st = 0 break elif a=="c" and b==3: print("平局") continue else: print("输入错误") continue def change0(): for i in range(0,13): list1.append(4) listpl.append(0) listco.append(0) def showp(): print() def judgel(sum,ca): global winner if(sum>11.5): if ca==1: winner=0 else: winner=1 showp() if(sum==11.5): if ca==1: winner=1 else: winner=0 showp() def player(): global player1 global listpl global pl global pll print("轮到你:") if pl==1: while 1: a=input() pll=0 if a=="a": c=0 while c==0: b=random.randint(0,12) if list1[b]!=0: list1[b]-=1; if b>=0 and b<=9: c=b+1; print(" 你摸到的牌是:",c) else: c=0.5 if b==10: print(" 你摸到的牌是:J", end=" ") elif b == 11: print(" 你摸到的牌是:Q", end=" ") elif b == 12: print(" 你摸到的牌是:K", end=" ") player1+=c listpl[b]+=1 judgel(player1,1) showp() break elif a=="s": pl=0 break else: print("输入错误") pll=1 else:print("你已停止摸牌") def computer(): global cl print("轮到电脑......") time.sleep(1) if cl==1: global computer1 global listco ra=random.uniform(1,11.5) op=11.5-computer1#安全区,[0,11.5],越大越安全 if op>=10:op=11.5 elif op>=9:op+=0.5 elif op>=8:op+=0.4 elif op>=7:op+=0.3 if ra<=op: c=0 while c==0: b=random.randint(0,12) if list1[b]!=0: list1[b]-=1; if b>=0 and b<=9: c=b+1; else: c=0.5 computer1+=c listco[b]+=1 judgel(computer1,0) else: print("电脑停止摸牌") cl=0 else: print("电脑停止摸牌") def main(): global player1 global computer1 global list1 global listpl global listco global st global winner global pl global cl global pll while 1: player1 = 0 computer1 = 0 list1=[] listpl = [] listco = [] st = 3 winner = 3 pl = 1 cl = 1 pll=1 change0() print("先通过石头剪刀布决定你和电脑谁先摸牌") stone() time.sleep(1) f_handler = open('out.log', 'w') oldstdout = sys.stdout sys.stdout = f_handler os.system('cls') sys.stdout=oldstdout if pll == 1: print("按下‘a’抽牌,按下's'停止抽牌") for i in range(0,5): if cl==0 and pl==0: break if winner!=3:break print("第",i+1,"轮摸牌:") print(" 目前你的牌:",end="") for i in range(0, 13): if listpl[i] != 0: a = listpl[i] while a != 0: if i >= 0 and i <= 9: print(i + 1, end=" ") elif i == 10: print("J", end=" ") elif i == 11: print("Q", end=" ") elif i == 12: print("K", end=" ") a -= 1 print() if st==1: player() if winner!=3:break computer() if winner != 3: break elif st==0: computer() if winner != 3: break player() if winner != 3: break time.sleep(1) f_handler = open('out.log', 'w') oldstdout = sys.stdout sys.stdout = f_handler os.system('cls') sys.stdout = oldstdout if winner==1: print("你赢了") elif winner==0: print("你输了") time.sleep(1) f_handler = open('out.log', 'w') oldstdout = sys.stdout sys.stdout = f_handler os.system('cls') sys.stdout = oldstdout if winner==3: if player1==computer1: print("平局") elif player1>computer1: print("你赢了") elif player1<computer1: print("你输了") print(" 目前你的牌:", end="") for i in range(0, 13): if listpl[i] != 0: a = listpl[i] while a != 0: if i >= 0 and i <= 9: print(i + 1, end=" ") elif i == 10: print("J", end=" ") elif i == 11: print("Q", end=" ") elif i == 12: print("K", end=" ") a -= 1 print(" 目前电脑的牌:", end="") for i in range(0, 13): if listco[i] != 0: a = listco[i] while a != 0: if i >= 0 and i <= 9: print(i + 1, end=" ") elif i == 10: print("J", end=" ") elif i == 11: print("Q", end=" ") elif i == 12: print("K", end=" ") a -= 1 print() print("按任意按键进入下一轮游戏") li=input() if instruct==1: instruction() main()
- 程序代码托管到码云
- 将代码传到Gitee上,方便代码保存和随时下载;
## 3. 实验过程中遇到的问题和解决过程
- 问题1:对pycharm英文的界面不习惯。
- 问题1解决方案:可以在设置-插件中下载中文插件,将英语界面换成熟悉的中文界面。但更恰当的方式是背单词,认识这些英文单词即可熟练地使用。
- 问题2:对黑色的界面不习惯。
- 问题2解决方案:在setting-外观中将主题从“Dark”换成“Light”,即可换成白色的界面。
- 问题3:对不清楚Git怎么用
- 问题3解决方案:通过询问同学和上网查找的方式,最终学会了上传代码
## 其他(感悟、思考等)
初高中曾经浅浅地了解了信息奥赛,虽然对C++浅尝辄止,但毕竟有了点编程的经验和知识,所以对Python的上手相对较快。Python在写法上比C++更加简洁,比如条件不用加小括号,函数不用加大括号之类的,更方便我们使用。相信在老师的引领下,通过上课听讲、看书自习、课后练习的方式,我一定能学好Python,掌握一门能助力自己工作、生活的编程语言。
## 参考资料
-《零基础学Python》