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

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

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

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

項目功能:

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

代碼如下:

import openpyxl
import random
class excel:
 def __init__(self,filename,sheetname):#讀某個單元格的值
  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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

秦皇岛市| 福建省| 湘乡市| 灵山县| 启东市| 泽州县| 方山县| 乐业县| 墨竹工卡县| 玉屏| 扎兰屯市| 宁都县| 高唐县| 晋中市| 崇州市| 邯郸县| 调兵山市| 衡水市| 江华| 米易县| 朝阳市| 名山县| 朝阳市| 鸡西市| 达孜县| 常宁市| 兴安盟| 绥中县| 孝昌县| 定襄县| 吉水县| 荃湾区| 多伦县| 高雄市| 罗江县| 澄城县| 资中县| 社旗县| 团风县| 思茅市| 福贡县|