python实现简易足球大战

from random import randint
import traceback

print('欢迎来到人机足球大战游戏,游戏开始...')
c_grade=0
u_grade=0
times=0
while times<10:
	if times%2==0:
		while True:
			try:
				try:
					attack=int(input('轮到你进攻,请输入你的进攻方向(1-5):\n'))
				except ValueError:
					print('操作选择错误!',end=' ')
					raise ValueError
				else:
					if attack<1 or attack>5:
						print('只能输入1-5之间的整数,请重新选择',end=' ')
						continue
					else:
						break
				finally:
					print('')
			except ValueError:
				print('*********traceback.print_exc*********')
				traceback.print_exc()
				print()
				print('只能输入整数!')
				error_log=traceback.format_exc()
				f=open('soccer_error_log.txt',encoding='utf-8',mode='a+')
				f.write(error_log)
				f.close()
				continue
		defeat=randint(1,5)
		if attack!=defeat:
			u_grade=u_grade+1
			print('进攻成功,得一分,恭喜!')
		else:
			print('进攻失败,很遗憾!')
			print('================================================================================')
	else:
		while True:
			try:
				try:
					defeat=int(input('轮到你防守,请输入你的防守方向(1-5):\n'))
				except ValueError:
					print('操作选择错误!',end=' ')
					raise ValueError
				else:
					if defeat<1 or defeat>5:
						print('只能输入1-5之间的整数,请重新选择',end=' ')
						continue
					else:
						break
				finally:
					print('')
			except ValueError:
				print('*********traceback.print_exc*********')
				traceback.print_exc()
				print()
				print('只能输入整数!')
				error_log=traceback.format_exc()
				f=open('soccer_error_log.txt',encoding='utf-8',mode='a+')
				f.write(error_log)
				f.close()
				continue
		attack=randint(1,5)
		if attack!=defeat:
			c_grade=c_grade+1 
			print('防守失败,电脑得一分,很遗憾!')
		else:
			print('防守成功,恭喜您!')
	print('您目前得分:%d   电脑目前得分%d'%(u_grade,c_grade))
	print('================================================================================')
	times=times+1
if u_grade>c_grade:
	print('恭喜您获胜!')
elif u_grade<c_grade:
	print('恭喜电脑获胜!')
elif u_grade==c_grade:
	print('平局!')
else:
	print('数据错误!')

你可能感兴趣的:(#,python)