笨办法学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有关的更多相关文章

  1. python学习03-数据类型

    一.基本数据类型--数字 布尔型 bool型只有两个值:True和False 之所以将bool值归类为数字,是因为我们也习惯用1表示True,0表示False. 以下是布尔值是False的各种情况: ...

  2. Python学习--03变量类型

    变量赋值 Python中的变量不需要声明,变量的赋值操作既是变量声明和定义的过程. 每个变量在内存中创建,都包括变量的标识,名称和数据这些信息. 每个变量在使用前都必须赋值,变量赋值以后该变量才会被创 ...

  3. python学习03

    字符串的基本使用 1.字符编码集 ASCII编码:外国人常用的大小写英文字母.数字和一些符号,一共127个字符,用1个字节(byte)可以涵盖完,也就是8个位,它将序列中的每个字节理解为一个字符. U ...

  4. python学习 03 函数 (只会执行一次return就不会往下执行)

    1.调用函数只会执行一次return,而且执行return后不会往下执行

  5. python学习03字符串基本操作

    '''字符串可以用单引号,双引号,三引号表示 '''#1.读取str1='I am a student!'#每一个字符对应一个下标,可以利用下标的方式来读取字符串对应的值——索引print(str1[ ...

  6. Python学习路程day16

    Python之路,Day14 - It's time for Django 本节内容 Django流程介绍 Django url Django view Django models Django te ...

  7. Python学习(二)Python 简介

    Python 简介 官方指南及文档 Python2.7官方指南(中文版):http://pan.baidu.com/s/1dDm18xr Python3.4官方指南(中文版):http://pan.b ...

  8. Python学习记录day6

    title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...

  9. Python学习记录day5

    title: Python学习记录day5 tags: python author: Chinge Yang date: 2016-11-26 --- 1.多层装饰器 多层装饰器的原理是,装饰器装饰函 ...

随机推荐

  1. mysql的分区和分表

    分区 分区就是把一个数据表的文件和索引分散存储在不同的物理文件中. mysql支持的分区类型包括Range.List.Hash.Key,其中Range比较常用: RANGE分区:基于属于一个给定连续区 ...

  2. php : 基础(5)

    函数 函数的定义: 形式: function 函数名 ( [$形参1] [,$形参2] [,.... ] ){ //函数体...... } 说明: 定义时使用的形参,其实就是一个变量--只能在该函数内 ...

  3. mac终端terminal快捷键:

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px "Hannotate SC" } span.s1 { } p.p1 { m ...

  4. linux建立一个快捷方式,连接到另一个目录

    sudo ln -s 源目录 目标快捷方式比如你要在/etc下面建立一个叫LXBC553的快捷方式,指向/home/LXBC,那就是sudo ln -s /home/LXBC /etc/LXBC553

  5. CentOS 安装 lamp(转)

    一般情况下,安装的都是最新的正式版,除非你有特殊需求,要安装指定的版本,本文暂不讨论.从最基础的开始,一点点完成一个可用的 Linux 主机.这里就开始介绍如何在 CentOS 6.0 上安装 LAM ...

  6. java aes_cbc_256 加密解密

    在之前我们在openssl上和ios上分别测试了 AES256位cbc模式的加密和解密 今天用java提供的api来测试一下:进而确定一下在PC,iOS,安卓上三个平台下的加密解密数据: 1. 首先通 ...

  7. 阿里云docker

    https://cr.console.aliyun.com docker 安装mysql image sudo docker pull  mysql docker  运行 mysql docker r ...

  8. HTML 的 meta 标签

    我们先来看看维基百科上对 meta element 的定义: Meta elements are tags used in HTML and XHTML documents to provide st ...

  9. ActiveReports中如何使用Excel数据源

    ActiveReports支持的数据源类型非常多,无论是常用的Oracle数据库.SQL Server.mySQL,还是开源的PostgreSQL,只要是具有ODBC驱动的数据库都可以正常使用,本文讲 ...

  10. JAVA06数组之动手动脑问题解决

     一.随机生成10个数,填充一个数组,然后用消息框显示数组内容,接着计算数组元素的和,将结果也显示在消息框中. 1.设计思路:首先生成10个随机数,然后存放至长度至少是10的数组中,然后计算10个随机 ...