Python猜字游戏

     有一个猜字游戏,现在的情况是,计算机随机生成一个整数,这个整数的区间我们事先给定,这里假定数在0到100之间,然后我们猜字,当猜的数大于生成的整数时,输出“Your answer is too large.”,当猜的数小于生成的整数时,输出“Your answer is too small.”,猜中数字输出“BINGO!!”,结束游戏,下面是python的脚本,以及测试情况:

def main():
	import random
	a=random.randint(0,100)
	print('Guess what i think?')
	b=int(input('Enter a number:'))
	while(b!=a):
		if(b<a):
			print('Your answer is too small.')
		elif(b>a):
			print('Your answer is too large.')
		b=int(input('Enter a number:'))
	print('BINGO!!')

测试结果:

>>> main()
Guess what i think?
Enter a number:0
Your answer is too small.
Enter a number:100
Your answer is too large.
Enter a number:50
Your answer is too large.
Enter a number:25
Your answer is too small.
Enter a number:37
Your answer is too small.
Enter a number:43
Your answer is too large.
Enter a number:40
Your answer is too small.
Enter a number:42
Your answer is too large.
Enter a number:41
BINGO!!

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值