笨办法学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. Download Oracle Forms 6i

    To download Oracle Forms Developer 6i from Oracle click this link http://download.oracle.com/otn/nt/ ...

  2. jq菜单折叠效果

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  3. Sprint(第九天11.22)

  4. 如何为数据源向导填加一种自定义的数据源类型(win示例)

    https://www.devexpress.com/Support/Center/Example/Details/T310160

  5. 单据UI代码开发

    1.构造UI项目后,打开生成的UI项目代码,在Model文件下,如初始化一些字段的值 2.订单明细行中行号设置.订单基本操作按钮提示UFIDA.U9.Base.BaseBP.Agent.dll(代理) ...

  6. Git_1基础操作,从安装到提交完成(windows)

    github地址:https://github.com/zhangsai521314/Git 1:安装Git Bash(https://git-scm.com/),安装一路NEXT. 2:目录架构: ...

  7. 尝试用md编辑器写博客, 内容为 jupyter笔记

    Matplotlib tutorial http://www.labri.fr/perso/nrougier/teaching/matplotlib/#introduction %matplotlib ...

  8. Win7全自动精简批处理_温柔处理极速修正版/暴力剩女工程测试版

    2011htpcfans 发表于 2012-5-11 http://bbs.wuyou.net/forum.php?mod=viewthread&tid=210269&highligh ...

  9. cookie封装

    //设置cookie function setCookie(name,value,days){     //如果不设置天数 , 默认为30天     days=days?days:30;     va ...

  10. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...