点球小游戏python脚本

yipeiwu_com5年前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))

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python基于有道实现英汉字典功能

本文实例讲述了Python基于有道实现英汉字典功能的方法。分享给大家供大家参考。具体如下: import re,urllib aa="http://dict.youdao.com/s...

Python Numpy库安装与基本操作示例

Python Numpy库安装与基本操作示例

本文实例讲述了Python Numpy库安装与基本操作。分享给大家供大家参考,具体如下: 概述 NumPy(Numeric Python)扩展包提供了数组功能,以及对数据进行快速处理的函...

Python实现一个数组除以一个数的例子

如果直接用python的一个list除以一个数,会报错: a = [1.0, 1.0, 1.0] c = a/3 print(c) TypeError: unsupported...

关于阿里云oss获取sts凭证 app直传 python的实例

首先安装需要的模块 pip install aliyun-python-sdk-core pip install aliyun-python-sdk-sts 获取需要的5个参数 g...

Python 多维List创建的问题小结

背景 最近在学Python,我觉得学习一个新语言最好的方式就是写一个简单的项目,所以就打算写一个简单的俄罗斯方块游戏。那么在写的过程中遇到了一个小问题。 def __init__...