In [ ]:
'''
函数及程序结构示例
猜数游戏1.0
几个函数的具体代码,没有给出,请大家自行设计字符图案。
'''
import random, math
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(
'''
======欢迎来到猜数游戏=======
/ \ , ,
_._ _ |oo| _ / \__/ \
_|||| ((/ () \)) / \
|||||/| ( ==== ) |oo|
\____/ _`\ /'_ / \
/ /.-' /\<>/\ `\.( () )_._
| ` / \/ \ /`'--'////)
\__,-'`| |. |\/ |/\/\|"\"`
| |. | \___/\___/
| |. | | |
======欢迎来到猜数游戏=======
'''
)
def menu():
print('''=====游戏菜单=====
1. 游戏说明
2. 开始游戏
3. 退出游戏
4. 制作团队
=====游戏菜单=====''')
def guess_game():
n = int(input('请输入一个大于0的整数,由计算机猜测'))
max_times = math.ceil(math.log(n, 2))
guess_times = 0
while guess_times <= max_times:
guess = random.randint(1, n)
print(guess)
guess_times += 1
print('一共可以猜', max_times, '次')
print('你已经猜了', guess_times, '次')
s = input('请输入你的判断:')
if s == '你答对了':
win()
print('神秘数字是:', guess)
print('你比标准次数少', max_times-guess_times, '次')
break
elif s == '猜大了':
guess = random.randint(1,guess - 1)
elif s == '猜小了':
guess = random.randint(guess + 1, n)
else:
print('神秘数字是:', n)
lose()
# 主函数
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()
#主程序
if __name__ == '__main__':
main()