python實戰之90行代碼寫個猜數字遊戲

一、導入庫

import random
import time

二、註冊用戶

我們用變量與input實現

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

三、註冊年齡

這裡我們得用except制作亂輸文本就遊戲結束的程序

亂輸文本就結束

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

順便把年齡設為30000[滑稽]

再根據年齡大小分配金幣

四、分配金幣

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,遊戲結束

五、if判斷 1

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

接下來就是最重要的遊戲部分瞭,認真!

我們還得用if語句判斷Game_start變量為True,並創建一個隨機數列表

六、if與隨機數

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('請猜數(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('隨機數為'+str(list_123))
	print('您現在有'+str(gold)+'金幣')
elif answer ==error_result:
	print('您輸瞭...')
	print('隨機數為'+str(list_123))
	gold = gold - 100
	print('您現在有'+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('您現在可以購買道具,是否購買?(True或False):'))
    if answer =='True':
        print('請說出要購買der道具')
        print('*'*41)
        print('*金幣翻倍器[現有的]   金幣翻倍器[得到的]*')
        print('*     2000G                  1500G      *')
        print('*'*41)
        answer = str(input('請說出要購買der道具(金幣翻倍器[現有的]說1,金幣翻倍器[得到的]說2):'))
        if gold >2000 and answer =='1':
            gold = gold - 2000
            answer = int(input('請說出要翻der倍數:'))
            gold = str(gold * answer)
            print('您現在有'+gold+'金幣')
            gold = int(gold)
        elif gold >1500 and answer =='2':
            gold = gold - 1500
            answer = int(input('請說出要翻der倍數:'))
            gold_mang = 100 * answer
            gold = gold + gold_mang
            print('您現在有'+str(gold)+'金幣')
            gold = int(gold)
            else:
                print('沒有這個道具,遊戲結束')
                gold = 10000
            elif answer =='False':
                print('繼續遊戲')
            else:
                print('非法輸入')
                gold = 10000

我寫到一半發現沒有把上面的橋接上,所以現在來橋接

十、橋接

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('請猜數(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('隨機數為'+str(list_123))
                print('您現在有'+str(gold)+'金幣')
            elif answer ==error_result:
                print('您輸瞭...')
                print('隨機數為'+str(list_123))
                gold = gold - 100
                print('您現在有'+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('您現在可以購買道具,是否購買?(True或False):'))
                if answer =='True':
                    print('請說出要購買der道具')
                    print('*'*41)
                    print('*金幣翻倍器[現有的]   金幣翻倍器[得到的]*')
                    print('*     2000G                  1500G      *')
                    print('*'*41)
                    answer = str(input('請說出要購買der道具(金幣翻倍器[現有的]說1,金幣翻倍器[得到的]說2):'))
                    if gold >2000 and answer =='1':
                        gold = gold - 2000
                        answer = int(input('請說出要翻der倍數:'))
                        gold = str(gold * answer)
                        print('您現在有'+gold+'金幣')
                        gold = int(gold)
                    elif gold >1500 and answer =='2':
                        gold = gold - 1500
                        answer = int(input('請說出要翻der倍數:'))
                        gold_mang = 100 * answer
                        gold = gold + gold_mang
                        print('您現在有'+str(gold)+'金幣')
                        gold = int(gold)
                    else:
                        print('沒有這個道具,遊戲結束')
                        gold = 10000
                elif answer =='False':
                    print('繼續遊戲')
                else:
                    print('非法輸入')
                    gold = 10000

其實到這裡我們就基本完成瞭,但如果隻寫到這裡會有很多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('請猜數(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('隨機數為'+str(list_123))
                print('您現在有'+str(gold)+'金幣')
            elif answer ==error_result:
                print('您輸瞭...')
                print('隨機數為'+str(list_123))
                gold = gold - 100
                print('您現在有'+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('您現在可以購買道具,是否購買?(True或False):'))
                if answer =='True':
                    print('請說出要購買der道具')
                    print('*'*41)
                    print('*金幣翻倍器[現有的]   金幣翻倍器[得到的]*')
                    print('*     2000G                  1500G      *')
                    print('*'*41)
                    answer = str(input('請說出要購買der道具(金幣翻倍器[現有的]說1,金幣翻倍器[得到的]說2):'))
                    if gold >2000 and answer =='1':
                        gold = gold - 2000
                        answer = int(input('請說出要翻der倍數:'))
                        gold = str(gold * answer)
                        print('您現在有'+gold+'金幣')
                        gold = int(gold)
                    elif gold >1500 and answer =='2':
                        gold = gold - 1500
                        answer = int(input('請說出要翻der倍數:'))
                        gold_mang = 100 * answer
                        gold = gold + gold_mang
                        print('您現在有'+str(gold)+'金幣')
                        gold = int(gold)
                    else:
                        print('沒有這個道具,遊戲結束')
                        gold = 10000
                elif answer =='False':
                    print('繼續遊戲')
                else:
                    print('非法輸入')
                    gold = 10000
            if gold ==0:
                print('金幣沒瞭,遊戲結束')
                gold = 10000
        elif Game_start =='False':
            print('歡迎遊玩,下次再見!')
            gold = 10000
        else:
            print('非法輸入')
            gold = 10000
print('遊戲結束,再見!')
time.sleep(10)

到此這篇關於python實戰之90行代碼寫個猜數字的文章就介紹到這瞭,更多相關python猜數字內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀:

    None Found