日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python文字游戏源代码求年纪_Python实现猜年龄游戏代码实例

發(fā)布時(shí)間:2023/12/1 python 52 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python文字游戏源代码求年纪_Python实现猜年龄游戏代码实例 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1. 在猜年齡的基礎(chǔ)上編寫登錄、注冊(cè)方法,并且把猜年齡游戲分函數(shù)處理,如

2. 登錄函數(shù)

3. 注冊(cè)函數(shù)

4. 猜年齡函數(shù)

5. 選擇獎(jiǎng)品函數(shù)

代碼如下

import json

real_age = 18

prize_list = ['好迪洗發(fā)水', '綠箭俠', '小豬佩奇', '布娃娃', '再來一次!']

import random

user_prize_dict = {}

import os

def register():

while True:

username = input('輸入用戶名>>>(q退出):').strip().lower()

if username=='q':break

password = input('請(qǐng)輸入密碼>>>:').strip()

re_password = input('請(qǐng)?jiān)俅未_認(rèn)密碼>>>:').strip()

if not password == re_password:

print('密碼不一致,請(qǐng)重輸!')

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('注冊(cè)成功!')

break

def login():

count = 0

while True:

if count == 3:

print('錯(cuò)誤輸入次數(shù)過多!')

break

username = input('請(qǐng)輸入用戶名>>>:').strip()

if not os.path.exists(username + '.txt'):

print('該用戶不存在!')

continue

password = input('請(qǐng)輸入密碼>>>:').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('用戶名或密碼錯(cuò)誤!')

count += 1

def guess_age():

count = 0

print('現(xiàn)在進(jìn)入猜年齡游戲環(huán)節(jié).......\n')

while True:

count += 1

if count == 4:

print('抱歉!你三次都猜錯(cuò)了!')

again_guess_age = input('請(qǐng)問是否要繼續(xù)猜3次(y繼續(xù),n退出)>>>:').strip().lower()

if again_guess_age == 'y':

count = 0

continue

break

age = input('請(qǐng)輸入你的年齡>>>:').strip()

if not age.isdigit():

print('請(qǐng)輸入純數(shù)字!')

continue

age = int(age)

if age > real_age:

print('猜大了!')

elif age < real_age:

print('猜小了!')

else:

print('恭喜你!猜對(duì)了!\n')

choice_prize()

break

def choice_prize():

count = 1

print('進(jìn)入抽獎(jiǎng)環(huán)節(jié).....,您共有兩次機(jī)會(huì)!\n 獎(jiǎng)品如下:')

while True:

for index, prize in enumerate(prize_list, 1):

print(index, prize)

choice = input('請(qǐng)按下按鈕y隨機(jī)選擇獎(jiǎng)品>>>:').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'本次獲得獎(jiǎng)品為:{prize},您還有{2-count}次機(jī)會(huì)!\n')

if count == 2:

if user_prize_dict.get('再來一次!'):

user_prize_dict.pop('再來一次!')

print(f'總共獲得的獎(jiǎng)品為:{user_prize_dict}')

break

count += 1

user_func_dic = {

'1': register,

'2': login,

}

while True:

print('''

先注冊(cè),登陸后才能玩猜年齡游戲哦!

1. 注冊(cè)

2. 登錄

'''

)

choice = input('請(qǐng)選擇功能編號(hào)(q退出)>>>:').strip().lower()

if choice == 'q' : break

if not choice in user_func_dic:

print('錯(cuò)誤輸入')

continue

user_func_dic.get(choice)()

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

本文標(biāo)題: Python實(shí)現(xiàn)猜年齡游戲代碼實(shí)例

本文地址: http://www.cppcns.com/jiaoben/python/303789.html

總結(jié)

以上是生活随笔為你收集整理的python文字游戏源代码求年纪_Python实现猜年龄游戏代码实例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。