笨办法学python第35节

该节主要是讲分支与函数,主要遇到的问题是python中如何判断输入是数字。

首先原代码如下:

from sys import exit

def gold_room():
print "This room is full of gold. How much do you take?" next = raw_input("> ")
if "" in next or "" in next:
how_much = int(next)
else:
dead("Man, learn to type a number.") if how_much < 50:
print "Nice, you're not greedy, you win!"
exit(0)
else:
dead("You greedy bastard!") def bear_room():
print "There is a bear here."
print "The bear has a bunch of honey."
print "The fat bear is in front of another door."
print "How are you going to move the bear?"
bear_moved = False while True:
next = raw_input("> ") if next == "take honey":
dead("The bear looks at you then slaps your face off.")
elif next == "taunt bear" and not bear_moved:
print "The bear has moved from the door. You can go through it now."
bear_moved = True
elif next == "taunt bear" and bear_moved:
dead("The bear gets pissed off and chews your leg off.")
elif next == "open door" and bear_moved:
gold_room()
else:
print "I got no idea what that means." def cthulhu_room():
print "Here you see the great evil Cthulhu."
print "He, it, whatever stares at you and you go insane."
print "Do you flee for your life or eat your head?" next = raw_input("> ")
if "flee" in next:
start()
elif "head" in next:
dead("Well that was tasty!")
else:
cthulhu_room() def dead(why):
print why, "Good job!"
exit(0) def start():
print "You are in a dark room."
print "There is a door to your right and left."
print "Which one do you take?" next = raw_input("> ") if next == "left":
bear_room()
elif next == "right":
cthulhu_room()
else:
dead("You stumble around the room until you starve.") start()

将改代码的流程图画出来思路就很清晰(略)(哈哈哈哈想起来小时候看答案,答案“略”,sad)

其中,gold_room函数的一个判断语句“if "0" in next or "1" in next:”,这句话就只能使得输入的数字中有1或0的才可以进行how_much的判断,那么python中有没有一种方法可以直接判断输入是否是数字,有哒,就是可以用语句“if  next.isdigit():”判断,将前一句换成这一句之后再运行,可以得到运行结果如下(其中我把raw_input("> ")换成了raw_input("please input a number: ")):

注:

补充:isdigit()方法检查字符串是否只包含数字(全由数字组成)。也就是说“isdigit()本身就是处理字符串的函数,他不分辨你到底是数字还是字符串”

除了判断raw_input()输入的是否是数字,还可以判断是否是字符串,如下

"if next.isdigit():" 都是数字
"if next.isalnum():" 都是数字或者字母
"if next.isalpha():" 都是字母
"if next.islower():" 都是小写
"if next.isupper():" 都是大写
"if next.istitle():" 都是首字母大写,像标题
"if next.isspace():" 都是空白字符、\t、\n、\r

(其他的判断还没有试,有用到的话回来找)

python学习——如何判断输入是数字的更多相关文章

  1. 用while判断输入的数字是否回文数

    /* Name:用while判断输入的数字是否回文数 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月18日 04:29:07 Description:用 ...

  2. python学习之判断和循环的使用

    作为一个小白运维,工作中常常发现很多东西还是自动化的好一点,所以就想到的用python来编写脚本.当然,我肯定是不会的啦,哈哈哈~~~~所以啦,身为一个懒癌晚期的上班族不得不在闲余时间来好好学学pyt ...

  3. js中判断输入的数字是否是数值类型

    p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 30.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...

  4. 猜数字游戏,判断输入的数字与系统产生的数字是否一致(Math.random()与if嵌套循环)

    package com.summer.cn; import java.util.Scanner; public class Test041509 { /** * java 随机数 Math * Mat ...

  5. Python3基础 if-else实例 判断输入的数字是否为8

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...

  6. Python如何规定对方输入的数字必须是整数?

    可以使用字符串str的isdigit方法判断字符串是否是一个仅有数字组成,也就是整数.如果是整数退出while循环,否则继续请求输入. 1 2 3 4 5 6 while True:     x =  ...

  7. Python 学习笔记(三)数字

    Python 数字 int 整型  是正或负整数  2 long 长整型  整数最后是一个大写或小写的L   2L float  浮点型 由整数部分和小数部分组成   2.0 complex 复数 小 ...

  8. python学习===从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。

    #!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': from sys import stdout filename ...

  9. Python练习笔记——对输入的数字进行加和

    请您输入数字,每个数字采用回车结束,当您输入型号*时,则结束数字输入,输出所有数字的总和 def num_sum(): i = 0 while True: get_num = input(" ...

随机推荐

  1. Obtaining Query Count Without executing a Query in Oracle D2k

    Obtaining Query Count Without executing a Query in Oracle D2k Obtaining a count of records that will ...

  2. Nexpose下载安装注册一条龙

    附上两个下载链接: Windows版本(64bit) : http://download2.rapid7.com/download/NeXpose-v4/NeXposeSetup-Windows64. ...

  3. 两个平行div之间有间隙

    两个平行的div使用 display: inline-block 会导致元素之间有空隙解决方法: 父元素设置 font-size: 0;

  4. CSS 制作三角形原理剖析

    使用css制作三角形其实原理很简单,下面一步步解析. 1.html代码如下 <div class="triangle"> </div> 2.CSS代码 .t ...

  5. JAVA 1.9 面向对象之封装

    1. 面向对象程序设计的三大基本特征:继承(Inheritence).封装(Encapsulation).多态(Polymorphism)2. 封装:类包含了数据与方法,将数据与方法放在一个类中就构成 ...

  6. Asp.NET利用ClientScript.RegisterStartupScript("")的同学,请注意!

    如果你想要在aspx.cs 文件用利用 ClientScript.RegisterStartupScript(""); 方法动态在DOM中执行脚本(比如想要将后置代码中的验证结果信 ...

  7. 流媒体测试笔记记录之————解决问题video.js 播放m3u8格式的文件,根据官方的文档添加videojs-contrib-hls也不行的原因解决了

    详细代码Github:https://github.com/Tinywan/PHPSharedLibrary/tree/master/Tpl/Html5/VideoJS 想播放hls协议的就是m3u8 ...

  8. session,ajax 跨域cookie

    什么是Session, 什么是Cookie? Session是由应用服务器维持的一个服务器端的存储空间,用户在连接服务器时,会由服务器生成一个唯一的SessionID,用该SessionID为标识符来 ...

  9. 從 Internet 安裝 Cygwin

    從 Internet 安裝 Cygwin 如果您有高速的 Internet 連線, 可以考慮用這個方法, 否則不建議使用 執行 setup.exe Cygwin Setup 畫面, 按 Next. C ...

  10. Android的系统体系结构

    目录: Android的系统体系结构 Android的四种常用组件 Activity的启动流程 Android的系统体系结构 在入门了一个简单的Android的Hello World以后,我们首先来看 ...