- 帖子
- 85
- 精华
- 0
- 积分
- 273
- 阅读权限
- 30
- 注册时间
- 2015-12-17
- 最后登录
- 2016-5-3
|
哼哧哼哧终于写出来了,自己run了两次,应该没有问题,就是感觉也没有比不加函数更简单,不过还是很有成就感的~~
另外,请教crossin老师,在定义函数参数的取值时,我是用if分别设定的,有没有办法比这个再简洁一点呢?- from random import choice
- score_you=0
- score_com=0
- direction=['left','center','right']
- def game(action, round_no):
- if action=='Shoot':
- pasttense1='shot'
- pasttense2='saved'
- small_l='shoot'
- result1='Goal!'
- result2='Oops...'
- if action=='Save':
- pasttense1='saved'
- pasttense2='shot'
- small_l='save'
- result1='Oops...'
- result2='Saved!'
- print '==== Round %d - You %s! ===='%(round_no,action)
- print 'Choose one side to %s: left, center, right'%(small_l)
- you=raw_input()
- print 'you %s '%pasttense1 + you
- com=choice(direction)
- print 'computer %s '%pasttense2 + com
- if you != com:
- print result1
- return 1
- else:
- print result2
- return 0
-
- for i in range(5):
- score_you += game('Shoot',i+1)
- print 'Score: %d(you) - %d(com)\n'%(score_you, score_com)
- score_com += game('Save',i+1)
- print 'Score: %d(you) - %d(com)\n'%(score_you, score_com)
复制代码 |
|