Python學習入門筆記(十八) 猜數(shù)字小游戲

程序介紹:
猜數(shù)字小游戲,計算機隨機生成一個1-100的數(shù)字,然后用戶猜數(shù)字的值,系統(tǒng)提示用戶:“數(shù)字太高了/數(shù)字太低了”。

代碼部分:

#原版1
import random

n = random.randint(1,100) #生成一個1-100的隨機整數(shù)
step = 0 #游戲的步數(shù)

print('Game start')
guess = int(input('Please enter an integer from 1 to 100:'))

while True:
    step+=1
    print('step',step)
    if guess<n :
        print(guess,'is low')
    elif guess>n:
        print(guess,'is high')
    else:
        print('You win!')
        break
    guess = int(input('Please enter an integer from 1 to 100:'))
    
print('Game over')

結(jié)果:
游戲存在bug,
1、輸入非數(shù)字字符會報錯
2、游戲沒有退出功能

#改進2  解決BUG 輸入0退出游戲
import random

n = random.randint(1,100) #生成一個1-100的隨機整數(shù)
step = 0 #游戲的步數(shù)
print('Game start')

def get_number():
    guess = input('Please enter an integer from 1 to 100:')
    while True:
        if guess.isdigit():#判斷輸入內(nèi)容是否是數(shù)字
            guess = int(guess)
            return guess
        else:
            guess = input('Please enter an integer from 1 to 100:')

guess = get_number()#獲取輸入數(shù)字

while True:
    step+=1
    print('step',step)
    
    if guess == 0:#退出游戲
        print('quit')
        break
    
    if guess<n :
        print(guess,'is low')
    elif guess>n:
        print(guess,'is high')
    else:
        print('You win!')
        break
    guess = get_number()
    
print('Game over')

結(jié)果2
Game start
Please enter an integer from 1 to 100:50
step 1
50 is high
Please enter an integer from 1 to 100:0 #按0退出游戲
step 2
quit
Game over

#改進3 增加提示,降低游戲難度,增加提示區(qū)間

import random

n = random.randint(1,100) #生成一個1-100的隨機整數(shù)
step = 0 #游戲的步數(shù)
high = 100
low = 1
print('Game start')

def get_number():
    guess = input('Please enter an integer from 1 to 100:')
    while True:
        if guess.isdigit():#判斷輸入內(nèi)容是否是數(shù)字
            guess = int(guess)
            return guess
        else:
            guess = input('Please enter an integer from 1 to 100:')

guess = get_number()

while True:
    step+=1
    print('step',step)
    
    if guess == 0:#退出游戲
        print('quit')
        break
    
    if guess<n :
        print(guess,'is low')
        low = guess + 1
    elif guess>n:
        print(guess,'is high')
        high = guess - 1
    else:
        print('You win!')
        break
    print('You can try',low,'to',high)
    guess = get_number()
    
print('Game over')

結(jié)果
Game start
Please enter an integer from 1 to 100:50
step 1
50 is low
You can try 51 to 100
Please enter an integer from 1 to 100:80
step 2
80 is high
You can try 51 to 79
Please enter an integer from 1 to 100:60
step 3
60 is high
You can try 51 to 59
Please enter an integer from 1 to 100:55
step 4
55 is high
You can try 51 to 54
Please enter an integer from 1 to 100:53
step 5
You win!
Game over

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容