python学习03——设计,与input有关
笨办法学python第36节,我写的代码如下:
from sys import exit def rule():
print "Congratulations! You made the right choice."
print "But is also a terrible choice."
print "You have to abide by the contract of the devil."
print "Input your choice: \n 1.dog \n 2.cat \n 3. panda" next = raw_input("> ") if next.isdigit():
next1 = int(next)
if next1 == 1:
print "Don't quarrel with me!"
elif next1 == 2:
print "Remember, good boyfriend won't quarrel with his girlfriend!"
elif next1 == 3:
print "Remember, don't quarrel with me!!!!!!!"
else:
print "Input a number in range(1,4)!" else:
print "please input a number"
exit(0) def start():
print "Do you want to be my boyfriend?"
print "input 'yes' or 'no' " next = raw_input("> ") if next == "yes":
rule()
elif next == "no":
print "How dare you!"
else:
print "You don't even know the rule, bye bye" start()
这个用的是raw_input("> "),下面的代码用的是input("> ")
from sys import exit def rule():
print "Congratulations! You made the right choice."
print "But is also a terrible choice."
print "You have to abide by the contract of the devil."
print "Input your choice: \n 1.dog \n 2.cat \n 3. panda" next = input("> ") if next in range(1,4): if next == 1:
print "Don't quarrel with me!"
elif next == 2:
print "Remember, good boyfriend won't quarrel with his girlfriend!"
else:
print "Remember, don't quarrel with me!!!!!!!" else:
print "please input a number in range(1,4)"
exit(0) def start():
print "Do you want to be my boyfriend?"
print "input 'yes' or 'no' " next = raw_input("> ") if next == "yes":
rule()
elif next == "no":
print "How dare you!"
else:
print "You don't even know the rule, bye bye" start()
注:
1. 第一个代码里面:next = raw_input("> "), 意思就是无论输入什么,都是字符串的类型,所以要在下面加上一句 next1 = int(next) 转换成数字进行判断。
2.第二个代码里面:用的 next = input("> "), 所以就不用 next.isdigit(): ,因为这个是对字符串进行的判断(参见上一节),这里的话就直接可以用 next in range(1,4):,但是应该注意的是在运行脚本的时候,如果输入字符串,要用“”引号引起来,否则会出现SyntaxError 。
3. 这两个函数均能接收字符串 ,但 raw_input() 直接读取控制台的输入(任何类型的输入它都可以接收)。而对于 input() ,它希望能够读取一个合法的 python 表达式,即你输入字符串的时候必须使用引号将它括起来,否则它会引发一个 SyntaxError 。
python学习03——设计,与input有关的更多相关文章
- python学习03-数据类型
一.基本数据类型--数字 布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. 以下是布尔值是False的各种情况: ...
- Python学习--03变量类型
变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创 ...
- python学习03
字符串的基本使用 1.字符编码集 ASCII编码:外国人常用的大小写英文字母.数字和一些符号,一共127个字符,用1个字节(byte)可以涵盖完,也就是8个位,它将序列中的每个字节理解为一个字符. U ...
- python学习 03 函数 (只会执行一次return就不会往下执行)
1.调用函数只会执行一次return,而且执行return后不会往下执行
- python学习03字符串基本操作
'''字符串可以用单引号,双引号,三引号表示 '''#1.读取str1='I am a student!'#每一个字符对应一个下标,可以利用下标的方式来读取字符串对应的值——索引print(str1[ ...
- Python学习路程day16
Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...
- Python学习(二)Python 简介
Python 简介 官方指南及文档 Python2.7官方指南(中文版):http://pan.baidu.com/s/1dDm18xr Python3.4官方指南(中文版):http://pan.b ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
- Python学习记录day5
title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
随机推荐
- 在Ubuntu下使用 csapp.h 和 csapp.c
它山之石可以攻玉. 对于<深入理解计算机系统>这本神人写就的神书, 我等凡人就不评论什么啦. 这本书的 第二,三 部分, 真的真的对我理解操作系统有很大的帮助. (当然, 如果你不看第一部 ...
- Javascript中的集合
集合是由一组无序且唯一(即不能重复)的项组成 function Set() { var items={}; this.has=function(value){ //return value in it ...
- App开发(Android与php接口)之:短信验证码
最近和同学们一起开发一个自主项目,要用到短信验证码,在网上搜索了很久,看到一个推荐贴,提到了很多不错的短信服务商.经过测试,帖子中提到的服务商他们的短信到达率和到达速度也都不错.最后,由于经费问题,我 ...
- easyx与VS2015
7.10 之前在文件头将__acrt_iob_func重定义&__iob_func,在格子涂色的程序中解决了问题:然而在俄罗斯方块的程序中出现了更多的问题,好像是FILE在其他外部依赖项cor ...
- C#: DataBase
using System.Data.SqlClient; namespace WindowsFormsApplication1{ class DB { private SqlConnection co ...
- ORM框架通过映射(反射)获取数据库的数据
ORM(Object Relational Mapping)框架采用元数据来描述对象一关系映射细节,元数据一般采用XML格式,并且存放在专门的对象一映射文件中.只要提供了持久化类与表的映射关系,ORM ...
- ireport5.6+jasperreport6.3开发(四)--以javabean为基准的报表开发(ireport)
javabean完成以后就是添加ireport的报表了 打开ireport的option然后在classpath中添加路径. 然后在ireport中追加数据源如图,点击图标后会出现数据源列表然后按ne ...
- java_ee_sdk-7u2的安装与 启动
1.在oracle官方网站上下载JAVA EE SDK,现在最新的版本是java_ee_sdk-7u2 2.最新的版本是免安装模式,所以比较方便.但是通常免安装模式没有快捷方式,不知道怎么启动.怎么使 ...
- div在不固定高度的情况下垂直或者水平居中
方法一: 用一个"ghost"伪元素(看不见的伪元素)和 inline-block / vertical-align 可以搞定居中,非常巧妙.但是这个方法要求待居中的元素是 inl ...
- (转)apache和nginx的区别
nginx 相对 apache 的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下ngin ...