Python實(shí)現(xiàn)猜年齡游戲代碼實(shí)例
1. 在猜年齡的基礎(chǔ)上編寫登錄、注冊方法,并且把猜年齡游戲分函數(shù)處理,如
2. 登錄函數(shù)
3. 注冊函數(shù)
4. 猜年齡函數(shù)
5. 選擇獎品函數(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('請輸入密碼>>>:').strip()
re_password = input('請?jiān)俅未_認(rèn)密碼>>>:').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('錯誤輸入次數(shù)過多!')
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('現(xiàn)在進(jìn)入猜年齡游戲環(huán)節(jié).......\n')
while True:
count += 1
if count == 4:
print('抱歉!你三次都猜錯了!')
again_guess_age = input('請問是否要繼續(xù)猜3次(y繼續(xù),n退出)>>>:').strip().lower()
if again_guess_age == 'y':
count = 0
continue
break
age = input('請輸入你的年齡>>>:').strip()
if not age.isdigit():
print('請輸入純數(shù)字!')
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('進(jìn)入抽獎環(huán)節(jié).....,您共有兩次機(jī)會!\n 獎品如下:')
while True:
for index, prize in enumerate(prize_list, 1):
print(index, prize)
choice = input('請按下按鈕y隨機(jī)選擇獎品>>>:').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}次機(jī)會!\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)()
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Python下的Softmax回歸函數(shù)的實(shí)現(xiàn)方法(推薦)
下面小編就為大家?guī)硪黄狿ython下的Softmax回歸函數(shù)的實(shí)現(xiàn)方法(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-01-01
Python實(shí)現(xiàn)備份文件實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)備份文件的方法,可實(shí)現(xiàn)針對各類常見擴(kuò)展名的文件進(jìn)行備份的功能,需要的朋友可以參考下2014-09-09
numpy按列連接兩個維數(shù)不同的數(shù)組方式
今天小編就為大家分享一篇numpy按列連接兩個維數(shù)不同的數(shù)組方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-12-12
通過python模糊匹配算法對兩個excel表格內(nèi)容歸類
這篇文章主要介紹了通過python模糊匹配算法對兩個excel表格內(nèi)容歸類,比如兩個不同的工程項(xiàng)目針對的對象都是A,那么就需要將這兩個工程項(xiàng)目歸類到A當(dāng)中,可以減少很大一部分工作量,,需要的朋友可以參考下2023-03-03
淺談Python Opencv中g(shù)amma變換的使用詳解
下面小編就為大家分享一篇淺談Python Opencv中g(shù)amma變換的使用詳解,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python OpenCV實(shí)現(xiàn)圖像模板匹配詳解
提供一個模板圖像,一個目標(biāo)圖像,且滿足模板圖像是目標(biāo)圖像的一部分,從目標(biāo)圖像中尋找特定的模板圖像的過程,即為模板匹配。本文將詳細(xì)講解如何利用Python OpenCV實(shí)現(xiàn)圖像模板匹配,需要的可以參考一下2022-04-04
Python爬蟲實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能案例
這篇文章主要介紹了Python爬蟲實(shí)現(xiàn)使用beautifulSoup4爬取名言網(wǎng)功能,結(jié)合實(shí)例形式分析了Python基于beautifulSoup4模塊爬取名言網(wǎng)并存入MySQL數(shù)據(jù)庫相關(guān)操作技巧,需要的朋友可以參考下2019-09-09

