Python练习---点球小游戏

今天,编写小游戏用python实现。
1.游戏要求:
设置球的方向:左中右三个方向,射门或者扑救动作,循环5次,直接输入方向。电脑随机挑选方向,如果方向相同,那么电脑得分,如果方向相反,那么人得分。
2.分析如何写程序:
1)循环,使用for ..in range()
2) if ..else
3)from random import choice 随机选择

3.脚本如下:

from random import choice
score_person=0
score_com=0
location=['left','center','right']

for i in range (5):
    print ("----Round %d You kicked----"%(i+1))
    com_choice=choice(location)
    print ("Computer's choice is %s"%com_choice)
    print ("input what your choice:left/center/right")
    you_choice=input()    
    print ("You have choose:"+you_choice)
    if you_choice!=com_choice:  # 方向不同,球进!
        score_person+=1    #人得分
        print ("Kicked!")
    else:
        print ("Saved unsuccesfully!")  #补救

    print ("Score:%d(person)-%d(com)\n" %(score_person, score_com))
    print ("----Round %d You saved----"%(i+1))
    com_choice=choice(location)
    print ("Computer's choice is %s"%com_choice)
    print ("input what your choice is:left/center/right")
    if you_choice==com_choice:  #方向相同,球不进!
        print ("Saved unsucessfully!")
        score_com+=1   #电脑得分
    else:
        print ("Kicked")
    print ("Score:%d(person)-%d(com)\n"%(score_person, score_com))

这小游戏的功能类似于猜数游戏。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值