In [1]:
#练习 1:求n个随机整数均值的平方根,整数范围在m与k之间。
import random,math

m = int(input('please input a smaller number     '))
k = int(input('please input a bigger number      '))
n = int(input('please input a  number for times  '))
i = 1
total = 0
avg = 0
num = random.randint(m,k)
print ('num0:',num)
total += num
while i < n :
    num = random.randint(m,k)
    print ('num',i,': ',num,sep='')
    total += num
    avg = total/2
    sqr = math.sqrt(avg)
    i += 1
print (n,'个随机整数均值的平方根等于:',sqr)


please input a smaller number     1
please input a bigger number      2
please input a  number for times  2
num0: 1
num1: 2
2 个随机整数均值的平方根等于: 1.224744871391589

In [2]:
#练习 2:共n个随机整数,整数范围在m与k之间,求西格玛log(随机整数)及西格玛1/log(随机整数)
import random,math

m = int(input('please input a smaller number  '))
k = int(input('please input a bigger number  '))
n = int(input('please input a  number for times  '))

num = random.randint(m,k)
print ('num0:',num)
num1 = math.log(num)
num2 = 1/math.log(num)
total1 = num1
total2 = num2
i = 1
while i < n :
    num = random.randint(m,k)
    print ('num',i,': ',num,sep='')
    num1 = math.log(num)
    num2 = 1/math.log(num)
    total1 += num1
    total2 += num2
    i += 1
print ('西格玛log总和为:',total1,'\n西格玛1/log总和为:',total2)


please input a smaller number  2
please input a bigger number  6
please input a  number for times  2
num0: 4
num1: 6
西格玛log总和为: 3.1780538303479453 
西格玛1/log总和为: 1.279458146995729

In [1]:
#练习 3:写函数,求s=a+aa+aaa+aaaa+aa...a的值,其中a是[1,9]之间的随机整数。
#例如2+22+222+2222+22222(此时共有5个数相加),几个数相加由键盘输入。
import random,math

a = random.randint(1,9)
print ('a:',a)
n = int(input('please input a  number for times  '))
print ('n:',n)

i = 1
b = a
total = a
while i < n :
    b = b*10+a
    
    total += b
    i += 1
print ('total:',total)


a: 5
please input a  number for times  3
n: 3
total: 615

In [3]:
#挑战性练习:将猜数游戏改成由用户随便选择一个整数,让计算机来猜测的猜数游戏。
import random,math

def show_instruction():
    print ('''将猜数游戏改成由用户随便选择一个整数,让计算机来猜测的猜数游戏
           如果计算机没有在规定的次数猜出你所输入的数,则你赢了,反之,你输了\n''')

def show_team():
    print ('team is lll\n')
    
def welcome():
    print(
        '''
           ======欢迎来到猜数游戏=======
        
                     /  \     ,    ,
           _._     _ |oo| _  / \__/ \
    
          _||||   ((/ () \))   /   \
    
          |||||/|  ( ==== )    |oo|    
           \____/  _`\  /'_    /   \
    
           /   /.-' /\<>/\ `\.( () )_._      
           |    `  /  \/  \  /`'--'////)
            \__,-'`|  |.  |\/ |/\/\|"\"` 
                   |  |.  | \___/\___/  
                   |  |.  |   |    |
          
          ======欢迎来到猜数游戏=======
        '''
    )
    
def win():     #计算机在规定的次数内没有猜出来
    print(
        '''
           ======恭喜你,你赢了=======
        
    
                ."".    ."",
                |  |   /  /
                |  |  /  /
                |  | /  /
                |  |/  ;-._ 
                }  ` _/  / ;
                |  /` ) /  /
                | /  /_/\_/\
                |/  /      |
                (  ' \ '-  |
                 \    `.  /
                  |      |
                  |      |
          
          ======恭喜你,你赢了=======
        '''
    )
    
def lose():  #计算机在规定的次数内猜出来了
    print(
        '''
           ======YOU LOSE=======
        
    
                

                   .-"      "-.
                  /            \
                 |              |
                 |,  .-.  .-.  ,|
                 | )(__/  \__)( |
                 |/     /\     \|
       (@_       (_     ^^     _)
  _     ) \_______\__|IIIIII|__/__________________________
 (_)@8@8{}<________|-\IIIIII/-|___________________________>
        )_/        \          /
       (@           `--------`
       
       
       
          ======YOU LOSE=======
        '''
    )
    
def game_over():   #次数用完计算机仍然没有猜出来
    print(
        '''
           ======GAME OVER=======
        
             _________ 
            / ======= \ 
           / __________\ 
          | ___________ | 
          | | -       | | 
          | |         | | 
          | |_________| |________________ 
          \=____________/                ) 
          / """"""""""" \               / 
         / ::::::::::::: \          =D-' 
        (_________________) 

       
          ======GAME OVER=======
        '''
    )
    
def mune():
    print('''             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====''')
    
def guess_game():
    m = int(input('请输入一个大于0的整数,作为计算机要猜的数,回车结束。'))
    n = int(input('请输入一个大于0的整数,作为神秘整数的上界,回车结束。'))
    max_times = math.ceil(math.log(n, 2))
    print('计算机可以猜的次数上限为:',max_times)
    i = 0 
    k = 1
    while i <  max_times:
        rand_num = random.randint(k,n)
        print('计算机第',i+1,'次猜的数为: ',rand_num)
        if rand_num == m:
            print ('由于计算机在规定的次数里猜出你所输入的数,所以......')
            lose()
            break
        elif rand_num > m:      #如果没有猜出则改变任意数的上下限以便猜中的几率增加
            n = rand_num
        else:
            k = rand_num
        i += 1
    if i >=  max_times:
        print ('由于计算机没有在规定的次数里猜出你所输入的数,所以......')
        win()  
    
def main():
    welcome()
    while True:
        mune()
        choice = int(input('请输入你的选择:'))
        if choice == 1:
            show_instruction()
        elif choice == 2:
            guess_game()
        elif choice == 3:
            game_over()
            break
        else:
            show_team()
            
main()


           ======欢迎来到猜数游戏=======
        
                     /  \     ,    ,
           _._     _ |oo| _  / \__/     
          _||||   ((/ () \))   /       
          |||||/|  ( ==== )    |oo|    
           \____/  _`\  /'_    /       
           /   /.-' /\<>/\ `\.( () )_._      
           |    `  /  \/  \  /`'--'////)
            \__,-'`|  |.  |\/ |/\/\|""` 
                   |  |.  | \___/\___/  
                   |  |.  |   |    |
          
          ======欢迎来到猜数游戏=======
        
             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
请输入你的选择:1
将猜数游戏改成由用户随便选择一个整数,让计算机来猜测的猜数游戏
           如果计算机没有在规定的次数猜出你所输入的数,则你赢了,反之,你输了

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
请输入你的选择:4
team is lll

             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
请输入你的选择:2
请输入一个大于0的整数,作为计算机要猜的数,回车结束。66
请输入一个大于0的整数,作为神秘整数的上界,回车结束。88
计算机可以猜的次数上限为: 7
计算机第 1 次猜的数为:  17
计算机第 2 次猜的数为:  69
计算机第 3 次猜的数为:  66
由于计算机在规定的次数里猜出你所输入的数,所以......

           ======YOU LOSE=======
        
    
                

                   .-"      "-.
                  /                             |              |
                 |,  .-.  .-.  ,|
                 | )(__/  \__)( |
                 |/     /\     \|
       (@_       (_     ^^     _)
  _     ) \_______\__|IIIIII|__/__________________________
 (_)@8@8{}<________|-\IIIIII/-|___________________________>
        )_/        \          /
       (@           `--------`
       
       
       
          ======YOU LOSE=======
        
             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
请输入你的选择:2
请输入一个大于0的整数,作为计算机要猜的数,回车结束。666
请输入一个大于0的整数,作为神秘整数的上界,回车结束。888
计算机可以猜的次数上限为: 10
计算机第 1 次猜的数为:  384
计算机第 2 次猜的数为:  422
计算机第 3 次猜的数为:  463
计算机第 4 次猜的数为:  728
计算机第 5 次猜的数为:  650
计算机第 6 次猜的数为:  726
计算机第 7 次猜的数为:  713
计算机第 8 次猜的数为:  689
计算机第 9 次猜的数为:  651
计算机第 10 次猜的数为:  675
由于计算机没有在规定的次数里猜出你所输入的数,所以......

           ======恭喜你,你赢了=======
        
    
                ."".    ."",
                |  |   /  /
                |  |  /  /
                |  | /  /
                |  |/  ;-._ 
                }  ` _/  / ;
                |  /` ) /  /
                | /  /_/\_/                |/  /      |
                (  ' \ '-  |
                 \    `.  /
                  |      |
                  |      |
          
          ======恭喜你,你赢了=======
        
             =====游戏菜单=====
                1. 游戏说明
                2. 开始游戏
                3. 退出游戏
                4. 制作团队
             =====游戏菜单=====
请输入你的选择:3

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

       
          ======GAME OVER=======