笨办法学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. python之实现基于paramiko和mysql数据库的堡垒机

    一.堡垒机结构 堡垒机执行流程: 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码) 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表 用户选择服务器,并自动登陆 ...

  2. MACS2 安装与使用

    1)下载MACS2 下载网址: https://pypi.python.org/pypi/MACS2 (有下载网址和安装.使用示例)   $ python setup.py install出现如下问题 ...

  3. Linux帮助命令

    帮助命令:man man 命令 获取命令的帮助 如下: [root@localhost ~]# man ls LS(1) User Commands LS(1) NAME ls - list dire ...

  4. Mac系统下配置Tomcat

    1.下载Tomcat,网址:http://tomcat.apache.org/download-70.cgi: 2.进入终端Terminal,打开配置文件.bash_profile,输入open .b ...

  5. 书__Oracle

    1.高可用性 1.1.ORACLE数据高可用之路 1.2.Oracle数据库进阶-高可用性.性能优化和备份恢复 2. 3. 4. 5.

  6. guardian keytab生成不了

    vim /var/kerberos/krb5kdc/kadm5.acl 将*e改成* /etc/init.d/kadmin restart 重启kadmin

  7. [官方作品] 关于ES4的设首页问题

    [官方作品] 关于ES4的设首页问题 Skyfree 发表于 2013-2-10 21:55:03 https://www.itsk.com/thread-254503-1-1.html 关于ES4设 ...

  8. Numpy Study 2----* dot multiply区别

    使用numpy时,跟matlab不同: 1.* dot() multiply() 对于array来说,* 和 dot()运算不同 *是每个元素对应相乘 dot()是矩阵乘法 对于matrix来说,* ...

  9. HashSet和HashMap的区别

    HashSet和HashMap的区别.Java的HashSet类是由哈希表支持.它不保证 set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用 null 元素.HashSet类为基本操作提供 ...

  10. LoadRunner脚本参数化设置

    LoadRunner,是一种预测系统行为和性能的负载测试工具,其中VUG(Virtual User Generator)主要用于虚拟用户生成和脚本编辑.为了实现单用户多次迭代执行脚本,VUG提供了强大 ...