请选择 进入手机版 | 继续访问电脑版
设为首页收藏本站

Crossin的编程教室

 找回密码
 立即加入
查看: 10538|回复: 5

第27课点球小游戏BUG?

[复制链接]

3

主题

0

好友

31

积分

新手上路

Rank: 1

发表于 2017-9-5 17:09:52 |显示全部楼层
本帖最后由 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
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-9-5 21:13:53 |显示全部楼层
你说的对,这个代码并不完善,你可以自行完善它
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

3

主题

0

好友

31

积分

新手上路

Rank: 1

发表于 2017-9-6 10:44:31 |显示全部楼层
本帖最后由 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.'

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

使用道具 举报

3

主题

0

好友

31

积分

新手上路

Rank: 1

发表于 2017-9-6 10:54:04 |显示全部楼层
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语句就可以了……
回复

使用道具 举报

3

主题

0

好友

31

积分

新手上路

Rank: 1

发表于 2017-9-6 10:58:55 |显示全部楼层
那我又有一个问题,判断 you 是不是 direction 的子集,有没有直接的判断语句,类似于集合中的包含于,这种符号?
回复

使用道具 举报

174

主题

45

好友

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

发表于 2017-9-7 14:54:30 |显示全部楼层
wujinyekong 发表于 2017-9-6 10:58
那我又有一个问题,判断 you 是不是 direction 的子集,有没有直接的判断语句,类似于集合中的包含于,这种 ...

if you in direction:
#==== Crossin的编程教室 ====#
微信ID:crossincode
网站:http://crossincode.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即加入

QQ|手机版|Archiver|Crossin的编程教室 ( 苏ICP备15063769号  

GMT+8, 2024-3-29 13:06 , Processed in 0.025567 second(s), 27 queries .

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部