python raw input函数中如何判断raw_input()输入的是数字?是判断,不是变成整型

在 Python 中如何判断输入数字是实数(整型数字或者浮点型数字)? - 知乎35被浏览6846分享邀请回答0添加评论分享收藏感谢收起python学习——如何判断输入是数字
时间: 18:59:03
&&&& 阅读:44
&&&& 评论:
&&&& 收藏:0
标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&笨办法学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 "0" in next or "1" in next:
how_much = int(next)
dead("Man, learn to type a number.")
if how_much & 50:
print "Nice, you‘re not greedy, you win!"
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()
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:
elif "head" in next:
dead("Well that was tasty!")
cthulhu_room()
def dead(why):
print why, "Good job!"
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()
dead("You stumble around the room until you starve.")
将改代码的流程图画出来思路就很清晰(略)(哈哈哈哈想起来小时候看答案,答案&略&,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: ")):
除了判断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
(其他的判断还没有试,有用到的话回来找)标签:&&&&&&&&&&&&&&&&&&&&&&&&&&&原文:/EiffelRachel/p/5865713.html
教程昨日排行
&&国之画&&&& &&&&&&
&& &&&&&&&&&&&&&&
鲁ICP备号-4
打开技术之扣,分享程序人生!首页 & perl/php/python/gawk/sedpython之raw_input和input 在使用python编写交互式程序时,经常用到的两个内部函数是raw_input和input(最常用还是input) ,本篇就通过一些示例看下两者之间的区别 。 一、raw_input 1.输入字符串nID = ''
nID = raw_input("Input your id plz")
if len(nID) != len("yang"):
print 'wring length of id,input again'
print 'your id is %s' % (nID) 2.输入整数nAge = int(raw_input("input your age plz:n"))
if nAge & 0 and nAge & 120:
print 'thanks!'
print 'bad age'
print 'your age is %dn' % nAge 3.输入浮点型fWeight = 0.0
fWeight = float(raw_input("input your weightn"))
print 'your weight is %f' % fWeight 4.输入16进制数据nHex = int(raw_input('input hex value(like 0x20):n'),16)
print 'nHex = %x,nOct = %dn' %(nHex,nHex) 5.输入8进制数据nOct = int(raw_input('input oct value(like 020):n'),8)
print 'nOct = %o,nDec = %dn' % (nOct,nOct) raw_input,默认是返回的类型是字符串型,如果想要返回其他类型的数据时,需要在语句前加上相应的数据类型(如int 、float)。 二、input input其实是通过raw_input来实现的,具体可以看python input的文档,原理很简单,就下面一行代码:def input(prompt):
return (eval(raw_input(prompt))) 再看一个示例:#!/usr/bin/python
# -*- coding: utf-8 -*-
# guess.py
import random
guessesTaken = 0
print ('Hello! what is your name')
#myName = raw_input()
myName = input()
number = random.randint(1, 23)
print ('well, ' + myName + ', i am thinking of a number between 1 and 23.')
while guessesTaken & 7:
print ('Take a guess')
guess = input()
#guess = int(guess)
guessesTaken = guessesTaken + 1
if guess & number:
print ('your guess is too low')
if guess & number:
print ('your guess is too high')
if guess == number:
if guess == number:
print ('good job! you guess in %d guesses!' %guessesTaken)
if guess != number:
print ('sorry! your guess is wrong')上面的程序在执行时,无论使用数字或字符串,执行第10行的数据输入时,如果不加引号时会报错。执行过程如下:使用int数字型
# python guess.py
Hello! what is your name
Traceback (most recent call last):
File "guess.py", line 12, in ?
print ('well, ' + myName + ', i am thinking of a number between 1 and 23.')
TypeError: cannot concatenate 'str' and 'int' objects
使用不加引号的string
# python guess.py
Hello! what is your name
Traceback (most recent call last):
File "guess.py", line 10, in ?
myName = input()
File "&string&", line 0, in ?
NameError: name 'yang' is not defined
使用加引号的string
# python guess.py
Hello! what is your name
well, yang, i am thinking of a number between 1 and 23.
Take a guess
your guess is too low
Take a guess
good job! you guess in 2 guesses! 三、raw_input与input的区别 &当输入为纯数字时 input返回的是数值类型,如int,float raw_inpout返回的是字符串类型,string类型 当输入字符串时, input会计算在字符串中的数字表达式,而raw_input不会。 如输入 “57 + 3”: input会得到整数60 raw_input会得到字符串”57 + 3”
本站的发展离不开您的资助,金额随意,欢迎来赏!
分类: perl/php/python/gawk/sed python您可能也喜欢python实现hp刀片ilo地址配置 钉钉webhook实现告警通知 python装饰器 python将某目录下所有excel文件合并 pyinstaller打包exe文件 捐助本站
如您感觉本博客有用,可扫码向本博客捐赠近期文章 python实现hp刀片ilo地址配置 shell实现hp刀片ilo地址配置 根据IP和掩码计算网段 shell实现netmask掩码和cidr掩码位转换 s权限位引发postfix及crontab异常处理文章归档 文章归档 选择月份 2017年九月 &(5) 2017年八月 &(3) 2017年七月 &(4) 2017年六月 &(2) 2017年五月 &(3) 2017年三月 &(3) 2017年二月 &(3) 2017年一月 &(4) 2016年十二月 &(4) 2016年十一月 &(6) 2016年十月 &(5) 2016年九月 &(5) 2016年八月 &(9) 2016年七月 &(5) 2016年六月 &(10) 2016年五月 &(18) 2016年四月 &(5) 2016年三月 &(4) 2016年二月 &(5) 2016年一月 &(8) 2015年十二月 &(8) 2015年十一月 &(9) 2015年十月 &(17) 2015年九月 &(10) 2015年八月 &(25) 2015年七月 &(11) 2015年六月 &(15) 2015年五月 &(23) 2015年四月 &(14) 2015年三月 &(22) 2015年二月 &(15) 2015年一月 &(24) 2014年十二月 &(13) 2014年十一月 &(16) 2014年十月 &(19) 2014年九月 &(19) 2014年八月 &(18) 2014年七月 &(20) 2014年六月 &(21) 2014年五月 &(24) 2014年四月 &(17) 2014年三月 &(29) 2014年二月 &(22) 2014年一月 &(22) 2013年十二月 &(24) 2013年十一月 &(20) 2013年十月 &(18) 2013年九月 &(16) 2013年八月 &(16) 2013年七月 &(20) 2013年六月 &(21) 2013年五月 &(19) 2013年四月 &(18) 2013年三月 &(24) 2013年二月 &(21) 2013年一月 &(18) 2012年十二月 &(24) 2012年十一月 &(18) 2012年十月 &(17) 2012年九月 &(17) 2012年八月 &(18) 2012年七月 &(26) 2012年六月 &(36) 2012年五月 &(36) 2012年四月 &(28) 2012年三月 &(46) 2012年二月 &(23) 2012年一月 &(15) 2011年十二月 &(27) 2011年十一月 &(59) 2011年十月 &(19) 2011年九月 &(16) 2011年八月 &(46)403 Forbidden
403 Forbidden

我要回帖

更多关于 raw input python3 的文章

 

随机推荐