1. 在猜年龄的基础上编写登录、注册方法,并且把猜年龄游戏分函数处理,如

2. 登录函数

3. 注册函数

4. 猜年龄函数

5. 选择奖品函数

代码如下

import json
real_age = 18
prize_list = ['好迪洗发水', '绿箭侠', '小猪佩奇', '布娃娃', '再来一次!']
import random
user_prize_dict = {}
import os

def register():
  while True:
    username = input('输入用户名>(q退出):').strip().lower()
    if username=='q':break
    password = input('请输入密码>:').strip()
    re_password = input('请再次确认密码>:').strip()
    if not password == re_password:
      print('密码不一致,请重输!')
      continue
    user_dic = {'name': username, 'password': password}
    json_user_dic = json.dumps(user_dic)
    with open(f"{username}.txt", 'w', encoding='utf-8')as f:
      f.write(json_user_dic)
      f.flush()
      print('注册成功!')
      break

def login():
  count = 0
  while True:
    if count == 3:
      print('错误输入次数过多!')
      break
    username = input('请输入用户名>:').strip()
    if not os.path.exists(username + '.txt'):
      print('该用户不存在!')
      continue
    password = input('请输入密码>:').strip()
    with open(f"{username}.txt", 'r', encoding='utf-8') as f:
      user_json_dic = f.read()
      user_dic = json.loads(user_json_dic)
      if username == user_dic['name'] and password == user_dic['password']:
        print('登录成功!')
        guess_age()
        break
      else:
        print('用户名或密码错误!')
    count += 1

def guess_age():
  count = 0
  print('现在进入猜年龄游戏环节.......\n')
  while True:
    count += 1
    if count == 4:
      print('抱歉!你三次都猜错了!')
      again_guess_age = input('请问是否要继续猜3次(y继续,n退出)>:').strip().lower()
      if again_guess_age == 'y':
        count = 0
        continue
      break
    age = input('请输入你的年龄>:').strip()
    if not age.isdigit():
      print('请输入纯数字!')
      continue

    age = int(age)
    if age > real_age:
      print('猜大了!')
    elif age < real_age:
      print('猜小了!')
    else:
      print('恭喜你!猜对了!\n')
      choice_prize()
      break

def choice_prize():
  count = 1
  print('进入抽奖环节.....,您共有两次机会!\n 奖品如下:')
  while True:
    for index, prize in enumerate(prize_list, 1):
      print(index, prize)
    choice = input('请按下按钮y随机选择奖品>:').strip().lower()
    if not choice == 'y':
      print('非法输入!')
      continue
    prize_choice = random.randint(1, 15)
    if prize_choice in [6, 7, 8]:
      prize_choice = 4
    elif prize_choice in [9, 10, 11, 12, 13, 14, 15]:
      prize_choice = 5
    prize = prize_list[prize_choice - 1]
    if prize in user_prize_dict:
      user_prize_dict[prize] += 1
    else:
      user_prize_dict[prize] = 1
    print(f'本次获得奖品为:{prize},您还有{2-count}次机会!\n')
    if count == 2:
      if user_prize_dict.get('再来一次!'):
        user_prize_dict.pop('再来一次!')
      print(f'总共获得的奖品为:{user_prize_dict}')
      break
    count += 1


user_func_dic = {
  '1': register,
  '2': login,
}
while True:
  print('''
    先注册,登陆后才能玩猜年龄游戏哦!
    1. 注册
    2. 登录
  '''
     )
  choice = input('请选择功能编号(q退出)>:').strip().lower()
  if choice == 'q' : break
  if not choice in user_func_dic:
    print('错误输入')
    continue
  user_func_dic.get(choice)()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

标签:
Python,猜年龄,游戏

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
白云城资源网 Copyright www.dyhadc.com

评论“Python实现猜年龄游戏代码实例”

暂无“Python实现猜年龄游戏代码实例”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。