笨办法学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. SQL2008触发器

    最近第一次接触触发器,感觉很是新奇,也很是蛋疼,因为老板要求的是在触发器中获取用户信息,并把对表的操作进行记录,后者实现到时比较简单,前者确实让我纠结了好久,其实百度了一下关于SQL2008触发器的文 ...

  2. A python script to check NE syncfail and get log from CIPS

    #! /usr/bin/env python # -*- coding: UTF-8 -*- """The script is to check whether NE i ...

  3. Hbase的配置和安装

    Hbase的配置和安装 1. 解压hbase.配置HBASE_HOME tar -zxvf hbase-1.2.4.tar.gz 2. 修改$HBASE_HOME/conf/hbase-env.sh文 ...

  4. TAG-9F10 发卡行相关数据(转)

    Tag-9F10  发卡行相关数据 9F10-发卡行应用数据 标签:9F10 字节1 –“ 07” 字节2 –DKI 长度:var10-25字节 字节3 – 密文版本号 格式:b 字节4 –“ 03” ...

  5. File system needs to be upgraded. You have version null and I want version 7

    安装hbase时候报错: File system needs to be upgraded. You have version null and I want version 7 注: 我安装的hba ...

  6. QQ号验证

    №☆★〇█962319751,1611385654,209414856,2729767026,1213235712,1187970122,1393756744,2463317013,346171055 ...

  7. string 常量池的理解

    1: String a="123"; String b="12"+"3"; String c="1"+"23& ...

  8. Oracle转MySQL

    1. to_date 直接去掉 例如 select log.id from CM_LOGINLOG log  where log.orgid =?  and log.isAuto =?  and lo ...

  9. java 反编译

    JavaDecompiler http://jd.benow.ca/jd-eclipse/update/

  10. Unity3D 之 iTween 相关

    有过 Flash 开发经验的朋友,对这个 iTween 应该感到非常熟悉吧,在 Flash 平台有一款功能几乎一样的插件 TweenLite,至于哪个先哪个后,有没有相互借鉴之类的就不在讨论范围了. ...