点球小游戏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设计】。

相关文章

简单了解django索引的相关知识

简单了解django索引的相关知识

前言 由于数据库每天都用来存储越来越多的信息,因此这些也是每个Django项目中的关键组件。 因此了解它们的工作方式非常重要。 当然,我无法解释所有可用于Django的不同数据库的全部细...

详解Python self 参数

1、概述 1.1 场景 我们在使用 Python 中的 方法 method 时,经常会看到 参数中带有 self,但是我们也没对这个参数进行赋值,那么这个参数到底是啥意思呢? 2、知识点...

Python之两种模式的生产者消费者模型详解

第一种使用queue队列实现: #生产者消费者模型 其实服务器集群就是这个模型 # 这里介绍的是非yield方法实现过程 import threading,time import...

详解pandas的外部数据导入与常用方法

外部数据导入 导入excel文件 pandas导入excel用read_excel()方法: import pandas as pd excel_file1 = pd.read_...

python英语单词测试小程序代码实例

这篇文章主要介绍了python英语单词测试小程序代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 爬取了扇贝英语网,并制作了一个...