• 计算机在1~1000间随机选取一个数字,然后告诉玩家要在某个限定次数内将这个数字猜出来。玩家每输入一个猜测值,计算机会告诉玩家,猜大了,小了还是对了,猜中则游戏结束,失败则游戏重新开始。 整个过程并不复杂,但既然是我们亲手做的第一个游戏,我们还是希望能把它做得有模有样,有欢迎页面,有菜单选择,有游戏说明、有GAME OVER页面,有猜中祝贺词,有游戏制作团队信息等。因此,我们把实现思路,大致步骤整理并记录下来,程序框架大概是下面这个样子:
      显示欢迎页面
      显示游戏菜单
      选择菜单玩游戏
      猜数过程
      显示GAME OVER

In [6]:
def win():
    print(
        '''
           ======恭喜你,你赢了=======
        
    
                ."".    ."",
                |  |   /  /
                |  |  /  /
                |  | /  /
                |  |/  ;-._ 
                }  ` _/  / ;
                |  /` ) /  /
                | /  /_/\_/\
                |/  /      |
                (  ' \ '-  |
                 \    `.  /
                  |      |
                  |      |
          
          ======恭喜你,你赢了=======
        '''
    )
def lose():
    print(
        '''
           ======YOU LOSE=======
        
    
                

                   .-"      "-.
                  /            \
                 |              |
                 |,  .-.  .-.  ,|
                 | )(__/  \__)( |
                 |/     /\     \|
       (@_       (_     ^^     _)
  _     ) \_______\__|IIIIII|__/__________________________
 (_)@8@8{}<________|-\IIIIII/-|___________________________>
        )_/        \          /
       (@           `--------`
       
       
       
          ======YOU LOSE=======
        '''
    )
    
import random,math
def guss_game () :
    n=int(input('please enter an integer as toplimit.  '))
    number=random.randint(1,n)
    max_times=math.ceil(math.log2(n))
    times=0
    
    while times<max_times :
        guss_num=int(input('please enter the number you guss.  '))
        times+=1
        print('一共可以猜', max_times, '次')
        print('你已经猜了', times, '次')
        
        if guss_num==number :
            win()
            print('神秘数字是:', number)
            print('你比标准次数少', max_times-times, '次')
            break
        elif guss_num>number :
            print('Sorry,you guss biger . ')
        else :
            print('Sorry,you guss samller . ')
    else :
        lose()
        print('this mystery number is ',number)

def menu ():
    print('''
             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====''')

def welcome():
    print(
        '''
           ======欢迎来到猜数游戏=======
        
                     /  \     ,    ,
           _._     _ |oo| _  / \__/ \
    
          _||||   ((/ () \))   /   \
    
          |||||/|  ( ==== )    |oo|    
           \____/  _`\  /'_    /   \
    
           /   /.-' /\<>/\ `\.( () )_._      
           |    `  /  \/  \  /`'--'////)
            \__,-'`|  |.  |\/ |/\/\|"\"` 
                   |  |.  | \___/\___/  
                   |  |.  |   |    |
          
          ======欢迎来到猜数游戏=======
        '''
    )
def game_over():
    print(
        '''
           ======GAME OVER=======
        
             _________ 
            / ======= \ 
           / __________\ 
          | ___________ | 
          | | -       | | 
          | |         | | 
          | |_________| |________________ 
          \=____________/                ) 
          / """"""""""" \               / 
         / ::::::::::::: \          =D-' 
        (_________________) 

       
          ======GAME OVER=======
        '''
    )
def game_instruction ():
    print('''
    =================================
    请输入一个数字作为猜数上限,
    然后根据指示输入您所猜数字,
    如果猜错则有您猜大了或小了的提示。
    =================================''')

def workteam ():
    print('''
    制作人:山羊不吃天堂草
    TEAM:    无敌小分队
    赞助商:  XXX特警''')
    
    
def main () :
    welcome()
    while True :
        menu()
        choice=int(input('please enter your choice. '))
        if choice==1 :
            game_instruction ()
        elif choice==2 :
            guss_game ()
        elif choice==3 :
            game_over() 
            break
        else :
            workteam()

if __name__ == '__main__':
    main()


           ======欢迎来到猜数游戏=======
        
                     /  \     ,    ,
           _._     _ |oo| _  / \__/     
          _||||   ((/ () \))   /       
          |||||/|  ( ==== )    |oo|    
           \____/  _`\  /'_    /       
           /   /.-' /\<>/\ `\.( () )_._      
           |    `  /  \/  \  /`'--'////)
            \__,-'`|  |.  |\/ |/\/\|""` 
                   |  |.  | \___/\___/  
                   |  |.  |   |    |
          
          ======欢迎来到猜数游戏=======
        

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 1

    =================================
    请输入一个数字作为猜数上限,
    然后根据指示输入您所猜数字,
    如果猜错则有您猜大了或小了的提示。
    =================================

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 2
please enter an integer as toplimit.  100
please enter the number you guss.  59
一共可以猜 7 次
你已经猜了 1 次
Sorry,you guss samller . 
please enter the number you guss.  80
一共可以猜 7 次
你已经猜了 2 次
Sorry,you guss biger . 
please enter the number you guss.  70
一共可以猜 7 次
你已经猜了 3 次
Sorry,you guss biger . 
please enter the number you guss.  60
一共可以猜 7 次
你已经猜了 4 次
Sorry,you guss samller . 
please enter the number you guss.  65
一共可以猜 7 次
你已经猜了 5 次
Sorry,you guss biger . 
please enter the number you guss.  62
一共可以猜 7 次
你已经猜了 6 次
Sorry,you guss biger . 
please enter the number you guss.  61
一共可以猜 7 次
你已经猜了 7 次

           ======恭喜你,你赢了=======
        
    
                ."".    ."",
                |  |   /  /
                |  |  /  /
                |  | /  /
                |  |/  ;-._ 
                }  ` _/  / ;
                |  /` ) /  /
                | /  /_/\_/                |/  /      |
                (  ' \ '-  |
                 \    `.  /
                  |      |
                  |      |
          
          ======恭喜你,你赢了=======
        
神秘数字是: 61
你比标准次数少 0 次

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 4

    制作人:山羊不吃天堂草
    TEAM:    无敌小分队
    赞助商:  XXX特警

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 4

    制作人:山羊不吃天堂草
    TEAM:    无敌小分队
    赞助商:  XXX特警

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 4

    制作人:山羊不吃天堂草
    TEAM:    无敌小分队
    赞助商:  XXX特警

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 3

           ======GAME OVER=======
        
             _________ 
            / ======= \ 
           / __________\ 
          | ___________ | 
          | | -       | | 
          | |         | | 
          | |_________| |________________ 
          \=____________/                ) 
          / """"""""""" \               / 
         / ::::::::::::: \          =D-' 
        (_________________) 

       
          ======GAME OVER=======
        
  • 练习 1:写函数,求n个随机整数均值的平方根,整数范围在m与k之间(n,m,k由用户输入)。

In [7]:
import random,math

n=int(input('please enter an integer as the number of integer you want to caculate . '))
m=int(input('please enter an integer as the downlimit.  '))
k=int(input('please enter an integer as the toplimit. '))
i=0
sum=0

while i<n :
    i+=1
    number=random.randint(m,k)
    sum+=number
    
print('the result is : ',math.sqrt(sum/n))


please enter an integer as the number of integer you want to caculate . 3
please enter an integer as the downlimit.  10
please enter an integer as the toplimit. 20
the result is :  3.7859388972001824
  • 练习 2:写函数,共n个随机整数,整数范围在m与k之间,(n,m,k由用户输入)。求1:西格玛log(随机整数),2:西格玛1/log(随机整数)

In [8]:
import random,math

n=int(input('please enter an integer as the number of integer you want to caculate . '))
m=int(input('please enter an integer as the downlimit.  '))
k=int(input('please enter an integer as the toplimit. '))
i=0
sum1=0
sum2=0

while i<n :
    i+=1
    number=math.log(random.randint(m,k))
    sum1+=number
    sum2+=1/number
    
print('the first result is :',sum1)
print('the second result is :',sum2)


please enter an integer as the number of integer you want to caculate . 4
please enter an integer as the downlimit.  100
please enter an integer as the toplimit. 200
the first result is : 19.970334269224008
the second result is : 0.8032496113626445
  • 练习 3:写函数,求s=a+aa+aaa+aaaa+aa...a的值,其中a是[1,9]之间的随机整数。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘输入。

In [10]:
import random

n=int(input('please enter an integer as the number of integer you want to add. '))
i=0
sum=0
a=random.randint(1,9)

while i<n :
    i+=1
    sum+=a
    a=a*10+a

print('the result is :',sum)


please enter an integer as the number of integer you want to add. 2
the result is : 84
  • 挑战性练习:仿照task5,将猜数游戏改成由用户随便选择一个整数,让计算机来猜测的猜数游戏,要求和task5中人猜测的方法类似,但是人机角色对换,由人来判断猜测是大、小还是相等,请写出完整的猜数游戏。

In [2]:
def menu ():
    print('''
             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====''')
def game_instruction ():
    print('''
    =================================
    请输入一个数字作为被猜数,
    然后计算机会输出它所猜数字,
    如果猜错则请您输入大了或小了的提示。
    =================================''')
def game_over():
    print('GAME OVER !SEE YOU NEXT TIME')
    
def workteam ():
    print('''
    制作人:山羊不吃天堂草
    TEAM:    无敌小分队
    赞助商:  XXX特警''')
def  welcome():
    print('WELCOME TO YOUR KINGDOM! ')

import random,math

def guss_game():
    m=int(input('please enter an integer as lowerlimit. '))
    n=int(input('please enter an integer as toplimit. '))
    flag_number=int(input('please enter the number you set. '))
    x=n-m
    max_times=int(math.log2(x))
    guss_times=0
    
    while guss_times<max_times :
        guss_times+=1
        c_guss=random.randint(m,n)
        print(c_guss)
        print('计算机一共可猜',max_times,'次')
        print('它已猜了',guss_times,'次')
        if c_guss==flag_number :
            print('Congratulation!the computer win!')
        elif c_guss>flag_number :
            input('c猜的数字')
            n=c_guss-1
        else :
            input('c猜的数字')
            m=c_guss+1
        print('他剩余',max_times-guss_times,'次')
    else :
        print('poor c, your loss.')

def main () :
    welcome()
    while True :
        menu()
        choice=int(input('please enter your choice. '))
        if choice==1 :
            game_instruction ()
        elif choice==2 :
            guss_game ()
        elif choice==3 :
            game_over() 
            break
        else :
            workteam()

if __name__ == '__main__':
    main()


WELCOME TO YOUR KINGDOM! 

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 1

    =================================
    请输入一个数字作为被猜数,
    然后计算机会输出它所猜数字,
    如果猜错则请您输入大了或小了的提示。
    =================================

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 2
please enter an integer as lowerlimit. 2
please enter an integer as toplimit. 20
please enter the number you set. 12
18
计算机一共可猜 4 次
它已猜了 1 次
c猜的数字is bigger
他剩余 3 次
13
计算机一共可猜 4 次
它已猜了 2 次
c猜的数字is bigger
他剩余 2 次
9
计算机一共可猜 4 次
它已猜了 3 次
c猜的数字is smaller
他剩余 1 次
11
计算机一共可猜 4 次
它已猜了 4 次
c猜的数字is smaller
他剩余 0 次
poor c, your loss.

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 4

    制作人:山羊不吃天堂草
    TEAM:    无敌小分队
    赞助商:  XXX特警

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
please enter your choice. 3
GAME OVER !SEE YOU NEXT TIME