最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)猜拳游戲項(xiàng)目

 更新時(shí)間:2020年11月30日 09:31:28   作者:江liuer_666  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)猜拳游戲項(xiàng)目,以excel形式保存信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)猜拳游戲的具體代碼,供大家參考,具體內(nèi)容如下

項(xiàng)目功能:

1.系統(tǒng)生成隨機(jī)的石頭剪刀布,玩家輸入石頭剪刀布
2.因?yàn)橥婕铱赡軙?huì)輸入shitou st這樣的輸入,需要格式化成合理輸入
3.進(jìn)行石頭剪刀布的游戲,輸出游戲結(jié)果,假設(shè)每次可以玩5局
4.將游戲結(jié)果寫入到excel中,包括系統(tǒng)出拳,玩家出拳,游戲結(jié)果,目前得分
5.游戲有歡迎信息(歡迎來到游戲)和用戶交互(游戲剩余次數(shù))
6.如果游戲的得分達(dá)到0分,游戲也會(huì)結(jié)束
7.在開始游戲的時(shí)候要求用戶輸入玩家姓名,會(huì)創(chuàng)建于玩家姓名同名的sheet頁
8.如果玩家已經(jīng)存在,則輸出歡迎回來,您目前的積分為:xx分
9.如果玩家不存在,則輸出歡迎來到游戲,您目前有5個(gè)積分
10.當(dāng)是老玩家,游戲積分為0分,則提示用戶充值,充值1元2積分

代碼如下:

import openpyxl
import random
class excel:
 def __init__(self,filename,sheetname):#讀某個(gè)單元格的值
  self.file = openpyxl.load_workbook(filename)
  self.sheet = self.file[sheetname]
  self.name=filename

 def write(self, sheet, data,num):#將數(shù)據(jù)以列表形式寫入
  sheet = self.file[sheet]
  for i in range(1, len(data) + 1):
   sheet.cell(num,i).value = data[i-1]
  self.file.save(self.name)

def formatx(indata):
 if indata=='shitou' or indata=='shi tou' or indata=='st':
  indata = '石頭'
 elif indata=='bu' or indata=='b u':
  indata = '布'
 elif indata=='jiandao' or indata=='jd':
  indata='剪刀'
 elif indata=='石頭' or indata=='布' or indata=='剪刀':
  pass
 return indata

def getscore(name):
 wb = openpyxl.load_workbook('first.xlsx')
 sheet = wb[name]
 maxrow = sheet.max_row
 maxcol = sheet.max_column
 score = sheet.cell(maxrow, maxcol).value
 if score=='積分':
  score = 5
  print("歡迎來到游戲")
 else:print("歡迎回來游戲")
 return score

def login(name):
 wb = openpyxl.load_workbook('first.xlsx')
 names = wb.sheetnames
 if name not in names:
  wb.create_sheet(name)
  sheet = wb[name]
  sheet.cell(1,1).value='電腦'
  sheet.cell(1, 2).value = '玩家'
  sheet.cell(1, 3).value = '結(jié)果'
  sheet.cell(1, 4).value = '積分'
  wb.save('first.xlsx')

if __name__=="__main__":
 name = input('請輸入您的名字:')
 login(name)
 score = getscore(name)
 print("積分{}".format(score))
 if score<=0:
  print('請充值:')
  money = int(input('請輸入充值金額'))
  score += money*2
 opt = excel('first.xlsx', name)
 for num in range(1,6):
  compute = random.choice(['石頭','剪刀','布'])
  player = input('請輸入猜拳的內(nèi)容:')
  player=formatx(player)
  if player==compute:
   result = [compute,player,'平局',score]
   print('電腦出拳:{},玩家出拳:{},游戲結(jié)果:{}'.format(compute,player,result[2],score))
   opt.write(name, result,num+1)
  elif (player=='石頭' and compute=='剪刀') or (player=='剪刀' and compute=='布') or player=='布' and compute=='石頭':
   score+=1
   result = [compute, player, '玩家勝利',score]
   print('電腦出拳:{},玩家出拳:{},游戲結(jié)果:{}'.format(compute, player, result[2],score))
   opt.write(name, result,num+1)
  else:
   score-=1
   result = [compute, player, '玩家失敗',score]
   print('電腦出拳:{},玩家出拳:{},游戲結(jié)果:{}'.format(compute, player, result[2],score))
   opt.write(name, result,num+1)
  if score<=0:
   break
  print('游戲剩余次數(shù):{}'.format(5-num))
 print("游戲結(jié)束")

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

乌什县| 四子王旗| 屏东县| 黎平县| 溧阳市| 漳平市| 奉化市| 通化县| 新兴县| 云霄县| 出国| 宜丰县| 全南县| 陵水| 赫章县| 虞城县| 西贡区| 通山县| 澎湖县| 赣州市| 斗六市| 白城市| 三明市| 增城市| 天津市| 灵寿县| 邢台市| 亚东县| 金川县| 沂源县| 龙泉市| 九龙县| 资源县| 浦城县| 泰来县| 邹平县| 临汾市| 涿鹿县| 临湘市| 德州市| 绩溪县|