Crossin的编程教室

标题: 第27课点球小游戏BUG? [打印本页]

作者: wujinyekong    时间: 2017-9-5 17:09
标题: 第27课点球小游戏BUG?
本帖最后由 wujinyekong 于 2017-9-5 17:12 编辑

from random import choice


score_you = 0

score_com = 0

direction = ['left', 'center', 'right']


for i in range(5):

   print '==== Round %d - You Kick! ====' % (i+1)

   print 'Choose one side to shoot:'
print 'left, center, right'
you = raw_input()

   print 'You kicked ' + you

   com = choice(direction)

   print 'Computer saved ' + com

   if you != com:

       print 'Goal!'
score_you += 1

   else:

       print 'Oops...'
print 'Score: %d(you) - %d(com)\n' % (score_you, score_com)


   print '==== Round %d - You Save! ====' % (i+1)

   print 'Choose one side to save:'
print 'left, center, right'
you = raw_input()

   print 'You saved ' + you

   com = choice(direction)

   print 'Computer kicked ' + com

   if you == com:

       print 'Saved!'
else:

       print 'Oops...'
score_com += 1

   print 'Score: %d(you) - %d(com)\n' % (score_you, score_com)


按照这个代码,可以随便写一个['left', 'center', 'right']之外的字符,都判定电脑没守中。

我想知道
if you != com:
       print 'Goal!'
score_you += 1
这个判断句说的啥?求指导

屏幕快照 2017-09-05 下午5.03.54.png (56.35 KB, 下载次数: 387)

屏幕快照 2017-09-05 下午5.03.54.png


作者: crossin先生    时间: 2017-9-5 21:13
你说的对,这个代码并不完善,你可以自行完善它
作者: wujinyekong    时间: 2017-9-6 10:44
本帖最后由 wujinyekong 于 2017-9-6 10:46 编辑
crossin先生 发表于 2017-9-5 21:13
你说的对,这个代码并不完善,你可以自行完善它

from random import choice

score = [0,0]
direction = ['left','center','right']

def kick():
    print '=====You Kick!====='
    print 'Choose one side to shoot:'
    print 'left,center,right'
    you = raw_input()
    print 'You kicked ' + you
    com = choice(direction)
    if you != 'left' and you != 'center' and you != 'right':
        print 'Game over!Spell right!'
        exit()

    else:
        if you == com:
            print 'Opps...'
        else:
            print 'Goal!'
            score[0]+=1
    print 'Score:%d(you)-%d(com)\n'%(score[0],score[1])

    print '=====You Save!====='
    print 'Choose one side to save'
    print 'left,center,right'
    you = raw_input()
    print 'You save ' + you
    com=choice(direction)
    if you != 'left' and you != 'center' and you != 'right':
        print 'Game over!Spell right!'
        exit()

    else:
        if you == com:
           print 'Saved!'
        else:
           print 'Opps...'
           score[1]+=1
    print 'Score:%d(you)-%d(com)\n'%(score[0],score[1])

for i in range(5):
    print '=====Round %d====='%(i+1)
    kick()

while(score[0]==score[1]):
    i +=1
    print'=====Round %d====='%(i+1)
    kick()

if score[0]>score[1]:
    print 'You Win!'
else:
    print 'You Lose.'

我加了判断语句,当出现非正确方向是结束程序。
可是我希望加一个语句,输入错误时,提示错误并重新重输入后继续。求指点一下,应该用什么语句?

作者: wujinyekong    时间: 2017-9-6 10:54
wujinyekong 发表于 2017-9-6 10:44
from random import choice

score = [0,0]

from random import choice

score = [0,0]
direction = ['left','center','right']

def kick():
    print '=====You Kick!====='
    print 'Choose one side to shoot:'
    print 'left,center,right'
    you = raw_input()
    while you != 'left' and you != 'center' and you != 'right':
        print 'Choose again!Spell right!'
        you = raw_input()
    print 'You kicked ' + you
    com = choice(direction
)
    if you == com:
        print 'Opps...'
    else:
        print 'Goal!'
        score[0]+=1
    print 'Score:%d(you)-%d(com)\n'%(score[0],score[1])

    print '=====You Save!====='
    print 'Choose one side to save'
    print 'left,center,right'
    you = raw_input()
    while you != 'left' and you != 'center' and you != 'right':
        print 'Choose again!Spell right!'
        you = raw_input()
    print 'You save ' + you
    com = choice(direction)

    if you == com:
        print 'Saved!'
    else:
        print 'Opps...'
        score[1]+=1
    print 'Score:%d(you)-%d(com)\n'%(score[0],score[1])

for i in range(5):
    print '=====Round %d====='%(i+1)
    kick()

while(score[0]==score[1]):
    i +=1
    print'=====Round %d====='%(i+1)
    kick()

if score[0]>score[1]:
    print 'You Win!'
else:
    print 'You Lose.'

自问自答了……有while语句就可以了……

作者: wujinyekong    时间: 2017-9-6 10:58
那我又有一个问题,判断 you 是不是 direction 的子集,有没有直接的判断语句,类似于集合中的包含于,这种符号?
作者: crossin先生    时间: 2017-9-7 14:54
wujinyekong 发表于 2017-9-6 10:58
那我又有一个问题,判断 you 是不是 direction 的子集,有没有直接的判断语句,类似于集合中的包含于,这种 ...

if you in direction:




欢迎光临 Crossin的编程教室 (https://bbs.crossincode.com/) Powered by Discuz! X2.5