In [2]:
#练习1#
import random,math
def average(m,n):
result = m/n
return result
m = int(input('请输入随机整数的下界,以回车结束。'))
k = int(input('请输入随机整数的上界,以回车结束。'))
n = int(input('请输入随机整数的个数,以回车结束。'))
i = 0
total = 0
while i < n:
i += 1
total += random.randint(m,k)
else:
print('结果是',math.sqrt(average(total,n)))
In [4]:
#练习2#
import random,math
m = int(input('请输入随机整数的下界,以回车结束。'))
k = int(input('请输入随机整数的上界,以回车结束。'))
n = int(input('请输入随机整数的个数,以回车结束。'))
i = 0
while i < n:
i += 1
print('西格玛log(随机整数)是:',math.log(random.randint(m,k)))
print('西格玛1/log(随机整数)是:',1/(math.log(random.randint(m,k))))
In [ ]:
import random, math
def main():
while True:
menu()
choice = int(input('请输入你的选择'))
if choice == 1:
show_instruction()
elif choice == 2:
guess_game()
elif choice == 3:
game_over()
break
else:
show_team()
def menu():
print('''=====游戏菜单=====
1. 游戏说明
2. 开始游戏
3. 退出游戏
4. 制作团队
=====游戏菜单=====''')
def win():
print( '''
======恭喜你,你赢了=======
."". ."",
| | / /
| | / /
| | / /
| |/ ;-._
} ` _/ / ;
| /` ) / /
| / /_/\_/\
|/ / |
( ' \ '- |
\ `. /
| |
| |
======恭喜你,你赢了======= ''')
def lose():
print( '''
======YOU LOSE=======
.-" "-.
/ \
| |
|, .-. .-. ,|
| )(__/ \__)( |
|/ /\ \|
(@_ (_ ^^ _)
_ ) \_______\__|IIIIII|__/__________________________
(_)@8@8{}<________|-\IIIIII/-|___________________________>
)_/ \ /
(@ `--------`
======YOU LOSE=======''')
def game_over():
print('''
======GAME OVER=======
_________
/ ======= \
/ __________\
| ___________ |
| | - | |
| | | |
| |_________| |________________
\=____________/ )
/ """"""""""" \ /
/ ::::::::::::: \ =D-'
(_________________)
======GAME OVER=======''')
def show_instruction():
print('请随便选择一个整数,由计算机来猜测,如果计算机在规定次数内猜中,则玩家输,反之,玩家赢')
def show_team():
print('北语16翻译本地化')
def guess_game():
m = 1
k = int(input('请输入一个大于0的整数,作为神秘整数的上界,以回车结束。'))
answer = int(input('请在1-1000的范围内输入一个整数,以回车结束。'))
guess_times = 0
max_times = math.ceil(math.log2(k))
while guess_times < max_times:
guess_times += 1
print('一共可以猜', max_times, '次', sep = '')
print('计算机已经猜了', guess_times, '次', sep = '')
now = random.randint(m,k)
print('计算机本次猜的结果是:',now)
if now == answer:
lose()
elif now > answer:
k = now
else:
m = now
else:
print('计算机没有在规定次数内猜对答案,恭喜你你赢了!')
win()
main()
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [20]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]:
In [ ]: