python学习——如何判断输入是数字
笨办法学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学习——如何判断输入是数字的更多相关文章
- 用while判断输入的数字是否回文数
/* Name:用while判断输入的数字是否回文数 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月18日 04:29:07 Description:用 ...
- python学习之判断和循环的使用
作为一个小白运维,工作中常常发现很多东西还是自动化的好一点,所以就想到的用python来编写脚本.当然,我肯定是不会的啦,哈哈哈~~~~所以啦,身为一个懒癌晚期的上班族不得不在闲余时间来好好学学pyt ...
- js中判断输入的数字是否是数值类型
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 30.0px Consolas; color: #2b7ec3 } p.p2 { margin: 0.0px ...
- 猜数字游戏,判断输入的数字与系统产生的数字是否一致(Math.random()与if嵌套循环)
package com.summer.cn; import java.util.Scanner; public class Test041509 { /** * java 随机数 Math * Mat ...
- Python3基础 if-else实例 判断输入的数字是否为8
镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...
- Python如何规定对方输入的数字必须是整数?
可以使用字符串str的isdigit方法判断字符串是否是一个仅有数字组成,也就是整数.如果是整数退出while循环,否则继续请求输入. 1 2 3 4 5 6 while True: x = ...
- Python 学习笔记(三)数字
Python 数字 int 整型 是正或负整数 2 long 长整型 整数最后是一个大写或小写的L 2L float 浮点型 由整数部分和小数部分组成 2.0 complex 复数 小 ...
- python学习===从键盘输入一些字符,逐个把它们写到磁盘文件上,直到输入一个 # 为止。
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': from sys import stdout filename ...
- Python练习笔记——对输入的数字进行加和
请您输入数字,每个数字采用回车结束,当您输入型号*时,则结束数字输入,输出所有数字的总和 def num_sum(): i = 0 while True: get_num = input(" ...
随机推荐
- JavaScript 中的对象
JavaScript 中的对象 在 JavaScript 中,对象是数据(变量),拥有属性和方法. JavaScript 中的所有事物都是对象:字符串.数字.数组.日期,等等. 访问对象的属性 访 ...
- 微信小程序-视图视图引用
引用 WXML 提供两种文件引用方式import和include. import import可以在该文件中使用目标文件定义的template,如: 在 item.wxml 中定义了一个叫item的t ...
- Kanzi Q&A
Q:为什么我的kanzi studio安装完后无法编译apk? A:请安装jdk,并设置环境变量,并更新thirdpartsdk. Q:为什么使用路径无法读取到列表listbox内的列表项? A:列表 ...
- SLAM前端技术选择思考
以前是专门做室内定位技术研究的,先后学习和分析了多种基于电磁的室内定位技术,如WiFi指纹定位(先后出现过RSSI.CTF.CIR多种指纹特征).WiFi ToF定位.低功耗蓝牙BLE以及iBeaco ...
- .NET下载文件报错System.UnauthorizedAccessException的解决方法
假设VS代码对应路径为E:\Projects\Web1,在VS用“发布Web”的方式发布后的路径为E:\Site\Web1.在IIS新建2个站点,站点A指向E:\Projects\Web1,站点B指向 ...
- target与currentTarget区别 ( html是弹窗居中的例子)
<!DOCTYPE html> <html> <head> <title></title> <style type="tex ...
- GitHub 用到的英语
1.Github首页Pull requests Issues Gist请求 问题 要点Learn Git and GitHub without any code!没有任何代码学习Git和GitHu ...
- 前端静态资源版本更新与缓存之——gulp自动化添加版本号
公司项目每次发布后,偶尔会有缓存问题,然后看了下gulp,发现gulp还能给js,css自动化添加版本号,可解决缓存的问题,所以自动化实现静态资源的版本更新才是正道.通过网上的资料试过了两种办法: 1 ...
- What is SSL and what are Certificates?
Refer to http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO/x64.html The content 1.2. What is SSL and ...
- Dev Express 动态生成XRTable使用总结
1. XRTableCell常见属性 XRTableCell xrTableCell = new XRTableCell(); A. 字体及字体大小 xrTableCell.Font = new S ...