主頁 > 知識庫 > python實(shí)戰(zhàn)之90行代碼寫個猜數(shù)字游戲

python實(shí)戰(zhàn)之90行代碼寫個猜數(shù)字游戲

熱門標(biāo)簽:浙江電銷卡外呼系統(tǒng)好用嗎 315電話機(jī)器人廣告 地圖制圖標(biāo)注位置改變是移位嗎 地圖標(biāo)注的意義點(diǎn) 房產(chǎn)電銷外呼系統(tǒng) 蓋州市地圖標(biāo)注 南京銷售外呼系統(tǒng)軟件 地圖標(biāo)注微信發(fā)送位置不顯示 上海機(jī)器人外呼系統(tǒng)哪家好

一、導(dǎo)入庫

import random
import time

二、注冊用戶

我們用變量與input實(shí)現(xiàn)

name = str(input('請輸入用戶名:'))
print('歡迎您,'+name)

三、注冊年齡

這里我們得用except制作亂輸文本就游戲結(jié)束的程序

亂輸文本就結(jié)束

try:
    age = int(input('請輸入年齡:'))
except ValueError:
    print('非法輸入')
    age = 30000

順便把年齡設(shè)為30000[滑稽]

再根據(jù)年齡大小分配金幣

四、分配金幣

if age 10:
    gold = 500
    print('您的初始金幣為500')
if age 20 and age >10:
    gold = 1000
    print('您的初始金幣為1000')
if age >20 and age 30:
    gold = 1500
    print('您的初始金幣為1500')
if age >30 and age 1000:
    gold = 200
    print('年齡過大,您的初始金幣為200')

用if語句保證金幣小于10000,如果等于10000,游戲結(jié)束

五、if判斷 1

if gold !=10000:
    Game_start = str(input('是否開始游戲?(True或False)'))
    while gold !=10000:

接下來就是最重要的游戲部分了,認(rèn)真!

我們還得用if語句判斷Game_start變量為True,并創(chuàng)建一個隨機(jī)數(shù)列表

六、if與隨機(jī)數(shù)

if Game_start =='True':
	list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
	while list_123 ==10:
		list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]

然后設(shè)定回答變量和正確答案與錯誤答案

七、回答、正確與錯誤

answer = str(input('請猜數(shù)(big或small):'))
if list_123[0] + list_123[1] + list_123[2] >10:
	result = 'big'
	error_result = 'small'
if list_123[0] + list_123[1] + list_123[2] 10:
	result = 'small'
	error_result = 'big'

還得用if語句判斷正確、錯誤與非法輸入的答案

八、if判斷 2

if answer ==result:
	print('您贏了!')
	gold = gold + 100
	print('隨機(jī)數(shù)為'+str(list_123))
	print('您現(xiàn)在有'+str(gold)+'金幣')
elif answer ==error_result:
	print('您輸了...')
	print('隨機(jī)數(shù)為'+str(list_123))
	gold = gold - 100
	print('您現(xiàn)在有'+str(gold)+'金幣')
else:
	print('非法輸入')
	gold = 10000

再制作購買道具的模塊

太長了我懶得寫一句一句解析

九、購買道具

if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
    answer = str(input('您現(xiàn)在可以購買道具,是否購買?(True或False):'))
    if answer =='True':
        print('請說出要購買der道具')
        print('*'*41)
        print('*金幣翻倍器[現(xiàn)有的]   金幣翻倍器[得到的]*')
        print('*     2000G                  1500G      *')
        print('*'*41)
        answer = str(input('請說出要購買der道具(金幣翻倍器[現(xiàn)有的]說1,金幣翻倍器[得到的]說2):'))
        if gold >2000 and answer =='1':
            gold = gold - 2000
            answer = int(input('請說出要翻der倍數(shù):'))
            gold = str(gold * answer)
            print('您現(xiàn)在有'+gold+'金幣')
            gold = int(gold)
        elif gold >1500 and answer =='2':
            gold = gold - 1500
            answer = int(input('請說出要翻der倍數(shù):'))
            gold_mang = 100 * answer
            gold = gold + gold_mang
            print('您現(xiàn)在有'+str(gold)+'金幣')
            gold = int(gold)
            else:
                print('沒有這個道具,游戲結(jié)束')
                gold = 10000
            elif answer =='False':
                print('繼續(xù)游戲')
            else:
                print('非法輸入')
                gold = 10000

我寫到一半發(fā)現(xiàn)沒有把上面的橋接上,所以現(xiàn)在來橋接

十、橋接

import random
import time
name = str(input('請輸入用戶名:'))
print('歡迎您,'+name)
try:
    age = int(input('請輸入年齡:'))
except ValueError:
    print('非法輸入')
    age = 30000
    gold = 10000
if age 10:
    gold = 500
    print('您的初始金幣為500')
if age 20 and age >10:
    gold = 1000
    print('您的初始金幣為1000')
if age >20 and age 30:
    gold = 1500
    print('您的初始金幣為1500')
if age >30 and age 1000:
    gold = 200
    print('年齡過大,您的初始金幣為200')
if gold !=10000:
    Game_start = str(input('是否開始游戲?(True或False)'))
    while gold !=10000:
        if Game_start =='True':
            list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            while list_123 ==10:
                list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            answer = str(input('請猜數(shù)(big或small):'))
            if list_123[0] + list_123[1] + list_123[2] >10:
                result = 'big'
                error_result = 'small'
            if list_123[0] + list_123[1] + list_123[2] 10:
                result = 'small'
                error_result = 'big'
            if answer ==result:
                print('您贏了!')
                gold = gold + 100
                print('隨機(jī)數(shù)為'+str(list_123))
                print('您現(xiàn)在有'+str(gold)+'金幣')
            elif answer ==error_result:
                print('您輸了...')
                print('隨機(jī)數(shù)為'+str(list_123))
                gold = gold - 100
                print('您現(xiàn)在有'+str(gold)+'金幣')
            else:
                print('非法輸入')
                gold = 10000
            if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
                answer = str(input('您現(xiàn)在可以購買道具,是否購買?(True或False):'))
                if answer =='True':
                    print('請說出要購買der道具')
                    print('*'*41)
                    print('*金幣翻倍器[現(xiàn)有的]   金幣翻倍器[得到的]*')
                    print('*     2000G                  1500G      *')
                    print('*'*41)
                    answer = str(input('請說出要購買der道具(金幣翻倍器[現(xiàn)有的]說1,金幣翻倍器[得到的]說2):'))
                    if gold >2000 and answer =='1':
                        gold = gold - 2000
                        answer = int(input('請說出要翻der倍數(shù):'))
                        gold = str(gold * answer)
                        print('您現(xiàn)在有'+gold+'金幣')
                        gold = int(gold)
                    elif gold >1500 and answer =='2':
                        gold = gold - 1500
                        answer = int(input('請說出要翻der倍數(shù):'))
                        gold_mang = 100 * answer
                        gold = gold + gold_mang
                        print('您現(xiàn)在有'+str(gold)+'金幣')
                        gold = int(gold)
                    else:
                        print('沒有這個道具,游戲結(jié)束')
                        gold = 10000
                elif answer =='False':
                    print('繼續(xù)游戲')
                else:
                    print('非法輸入')
                    gold = 10000

其實(shí)到這里我們就基本完成了,但如果只寫到這里會有很多bug,不想看了也可以就玩這個版本

還剩了一些代碼,我也懶得寫了,直接完整代碼

十一、完整代碼

import random
import time
name = str(input('請輸入用戶名:'))
print('歡迎您,'+name)
try:
    age = int(input('請輸入年齡:'))
except ValueError:
    print('非法輸入')
    age = 30000
    gold = 10000
if age 10:
    gold = 500
    print('您的初始金幣為500')
if age 20 and age >10:
    gold = 1000
    print('您的初始金幣為1000')
if age >20 and age 30:
    gold = 1500
    print('您的初始金幣為1500')
if age >30 and age 1000:
    gold = 200
    print('年齡過大,您的初始金幣為200')
if gold !=10000:
    Game_start = str(input('是否開始游戲?(True或False)'))
    while gold !=10000:
        if Game_start =='True':
            list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            while list_123 ==10:
                list_123 = [random.randint(1,6),random.randint(1,6),random.randint(1,6),]
            answer = str(input('請猜數(shù)(big或small):'))
            if list_123[0] + list_123[1] + list_123[2] >10:
                result = 'big'
                error_result = 'small'
            if list_123[0] + list_123[1] + list_123[2] 10:
                result = 'small'
                error_result = 'big'
            if answer ==result:
                print('您贏了!')
                gold = gold + 100
                print('隨機(jī)數(shù)為'+str(list_123))
                print('您現(xiàn)在有'+str(gold)+'金幣')
            elif answer ==error_result:
                print('您輸了...')
                print('隨機(jī)數(shù)為'+str(list_123))
                gold = gold - 100
                print('您現(xiàn)在有'+str(gold)+'金幣')
            else:
                print('非法輸入')
                gold = 10000
            if gold ==2000 or gold ==3000 or gold ==4000 or gold ==5000 or gold ==6000 or gold ==7000 or gold ==8000 or gold ==9000:
                answer = str(input('您現(xiàn)在可以購買道具,是否購買?(True或False):'))
                if answer =='True':
                    print('請說出要購買der道具')
                    print('*'*41)
                    print('*金幣翻倍器[現(xiàn)有的]   金幣翻倍器[得到的]*')
                    print('*     2000G                  1500G      *')
                    print('*'*41)
                    answer = str(input('請說出要購買der道具(金幣翻倍器[現(xiàn)有的]說1,金幣翻倍器[得到的]說2):'))
                    if gold >2000 and answer =='1':
                        gold = gold - 2000
                        answer = int(input('請說出要翻der倍數(shù):'))
                        gold = str(gold * answer)
                        print('您現(xiàn)在有'+gold+'金幣')
                        gold = int(gold)
                    elif gold >1500 and answer =='2':
                        gold = gold - 1500
                        answer = int(input('請說出要翻der倍數(shù):'))
                        gold_mang = 100 * answer
                        gold = gold + gold_mang
                        print('您現(xiàn)在有'+str(gold)+'金幣')
                        gold = int(gold)
                    else:
                        print('沒有這個道具,游戲結(jié)束')
                        gold = 10000
                elif answer =='False':
                    print('繼續(xù)游戲')
                else:
                    print('非法輸入')
                    gold = 10000
            if gold ==0:
                print('金幣沒了,游戲結(jié)束')
                gold = 10000
        elif Game_start =='False':
            print('歡迎游玩,下次再見!')
            gold = 10000
        else:
            print('非法輸入')
            gold = 10000
print('游戲結(jié)束,再見!')
time.sleep(10)

到此這篇關(guān)于python實(shí)戰(zhàn)之90行代碼寫個猜數(shù)字的文章就介紹到這了,更多相關(guān)python猜數(shù)字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python生命游戲?qū)崿F(xiàn)原理及過程解析(附源代碼)
  • 詳解Python 實(shí)現(xiàn)元胞自動機(jī)中的生命游戲(Game of life)
  • python實(shí)現(xiàn)生命游戲的示例代碼(Game of Life)
  • python實(shí)現(xiàn)21點(diǎn)小游戲
  • 教你用Python寫一個植物大戰(zhàn)僵尸小游戲
  • 教你如何用python開發(fā)一款數(shù)字推盤小游戲
  • 用Python寫一個簡易版彈球游戲
  • python實(shí)現(xiàn)簡單反彈球游戲
  • Python實(shí)戰(zhàn)之實(shí)現(xiàn)康威生命游戲

標(biāo)簽:貴州 日照 赤峰 金華 陽泉 雙鴨山 臨汾 克拉瑪依

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python實(shí)戰(zhàn)之90行代碼寫個猜數(shù)字游戲》,本文關(guān)鍵詞  python,實(shí)戰(zhàn),之,90行,代碼,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《python實(shí)戰(zhàn)之90行代碼寫個猜數(shù)字游戲》相關(guān)的同類信息!
  • 本頁收集關(guān)于python實(shí)戰(zhàn)之90行代碼寫個猜數(shù)字游戲的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章