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.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...
随机推荐
- 兼容IE7音乐播放器之jplayer的使用
首先列出为何要写这篇随笔的原因: 1:兼容IE7 2:音乐播放器 3:任意控制播放器 1: 最近做的网站需要兼容IE7,在此之前已经写好了关于音乐播放的插件,火狐,IE8以上,以及谷歌浏览器等都可以随 ...
- Linux关机和重启命令
shutdown shutdown [选项] 时间 选项: -c : 取消一个关机命令 -h : 关机 -r : 重启 [root@localhost ~]# date Tue Dec 6 21:06 ...
- MYSQL limit,offset 区别
SELECT keyword FROM keyword_rank WHERE advertiserid' order by keyword LIMIT OFFSET ; 比如这个SQL ,limit后 ...
- 合并excel中多个sheet
Sub 合并当前工作簿下的所有工作表() Application.ScreenUpdating = False For j = 1 To Sheets.Count If Sheets(j).Name ...
- LINQ之路 8: 解释查询(Interpreted Queries)
LINQ提供了两个平行的架构:针对本地对象集合的本地查询(local queries),以及针对远程数据源的解释查询(Interpreted queries). 在讨论LINQ to SQL等具体技术 ...
- redis安装
参考redis.io 安装步骤 下载,编译reids $ wget http://download.redis.io/releases/redis-3.0.6.tar.gz $ tar xzf red ...
- LayoutInflater的使用
在实际工作中,事先写好的布局文件往往不能满足我们的需求,有时会根据情况在代码中自定义控件,这就需要用到LayoutInflater.LayoutInflater在Android中是“扩展”的意思,作用 ...
- MongoDB 概念解析
SQL术语/概念 MongoDB术语/概念 解释/说明 database database 数据库 table collection 数据库表/集合 row document 数据记录行/文档 col ...
- js无刷新上传图片,服务端有生成缩略图,剪切图片,iphone图片旋转判断功能
html: <form action="<{:AppLink('circle/uploadimg')}>" id="imageform" me ...
- Python学习---除法
python有两种除法,普通除法 a/b ,不论a,b精度 得到的都是浮点数. 4/2 = 2.0 3/5 = 0.6 floor除法,a//b , 得到一个舍弃小数位的整数结果,所以结果永远是 ...