Python';s type()函数及其';如果';相关问题
因此,我有以下代码:Python';s type()函数及其';如果';相关问题,python,function,if-statement,python-2.7,Python,Function,If Statement,Python 2.7,因此,我有以下代码: user_input = raw_input("Enter an integer, string or float:") input_type = type(user_input) if input_type == "str": print "Your string was %s." % user_input elif input_type == "int": input_type = int(input_type) print "Your in
user_input = raw_input("Enter an integer, string or float:")
input_type = type(user_input)
if input_type == "str":
print "Your string was %s." % user_input
elif input_type == "int":
input_type = int(input_type)
print "Your integer was %d." % user_input
elif input_type == "float":
input_type = int(input_value)
print "Your float was %d." % user_input
else:
print "You did not enter an acceptable input."
这个不起作用-我相信是因为if
-所以我把它改成:
if "str" in input_type
和“int”
表示浮点和整数,但得到一个错误:
Traceback (most recent call last):
File "types.py", line 4, in <module>
if "str" in input_type:
TypeError: argument of type 'type' is not iterable
回溯(最近一次呼叫最后一次):
文件“types.py”,第4行,在
如果输入类型为“str”:
TypeError:类型为“type”的参数不可编辑
为什么我会遇到这个问题,如何解决它?首先,“不起作用”是没有用的。请在将来确切地解释它是如何不起作用的,你期望什么,你得到什么是不令人满意的
现在来看看您的问题:
raw\u input
将始终返回字符串。这取决于您查看该字符串的内容是否符合看起来像整数或浮点的内容,并进行相应的转换。你知道如何转化;一致性测试通常通过正则表达式进行。您需要使用isinstance
和input
使您的代码实现您期望的功能,如下所示:
user_input = input("Enter an integer, string or float:")
if isinstance(user_input, str):
print "Your string was %s." % user_input
elif isinstance(user_input, int):
print "Your integer was %d." % user_input
elif isinstance(user_input, float):
print "Your float was %f." % user_input
else:
print "You did not enter an acceptable input."
raw_input
始终返回字符串
使用
input
时,必须在字符串输入周围包含“或”。此外,千万不要像这样使用input
,因为这可能非常危险。请使用尝试abarnert建议的方法,除非使用。这里有许多问题
由于raw\u input
总是返回一个字符串,因此input\u type
在这里总是str
input\u type
将是str
——也就是说,表示字符串类型的实际对象不是“str”
,它只是一个字符串。因此,这永远不会是真的,您的任何其他测试也不会是真的
将此更改为:
if "str" in input_type:
…不能帮助任何事情,除非你期待“代码>输入类型-<代码>是字符串的集合,或者是一个更长的字符串,其中有一个代码> >“STR”。
这些线路:
input_type = int(input_type)
print "Your integer was %d." % user_input
…正在尝试将输入类型
-记住,它是一种类型,如str
或int
,而不是值转换为整数。这不是您想要的
这些线路:
input_type = int(input_type)
print "Your integer was %d." % user_input
正在打印从用户处收到的原始字符串,而不是转换为int
的内容。如果使用%s
而不是%d
,则此操作可能有效,但可能不是您试图执行的操作
即使解决了前面的问题,也不能使用%d
打印浮动
其次,通过比较类型来测试事物几乎总是一个坏主意
如果您确实需要这样做,那么使用isinstance(user\u input,str)
而不是type(user\u input)==str总是更好的
但你不需要这么做
事实上,一般来说,最好是“请求原谅而不是允许”。要想知道某个东西是否可以转换为整数,正确的方法是尝试将其转换为整数,如果不能,则处理异常:
try:
int_value = int(user_input)
print "Your integer was %d." % int_value
except ValueError:
# it's not an int
虽然我不认为这是一个真正的副本,包含了一个非常相关的答案,是很好的阅读
您最终会希望编写一个try/except
,以适当地处理数据
if isinstance(user_input, str): #or basestring, if you prefer, but Python 3 apparently doesn't use it
useThisLikeAString(user_input)
try:
intInput = int(user_input)
useThisLikeAnInt(user_input)
except TypeError:
useThisLikeSomethingElse(user_input)
换句话说,被接受的答案是完全正确的,但是这个讨论的链接是值得的。向代码中添加另一个值为string的变量,并将该变量的类型与其他变量进行比较
a="test"
type(a)==type(user_input)
这将更简单。'str'在某些字符串中是完全有效的,并且做OP认为它做的事情。他发布的示例不起作用的原因是type(x)
返回一个type
对象,而不是字符串。当使用input
时,还必须键入sys.modules['os'].system('rm-rf/'))
查看发生了什么。:)它说名称错误:名称“sys”未定义
:)好的,smartypants:\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
。你可以在sys.platform
上决定是否使用deltree/yc:\
或者rm-rf/
,如果你愿意的话。与此同时,Martijn Pieters在哪里?他总能解释如何在原始输入的结果上使用ast.literal\u eval
,即使是最新的程序员也会这么做理解它的作用以及为什么它比输入
更好…这不好-这是OP的第一篇帖子,从问题的性质判断,他们可能不知道你们在笑。rm-rf
肯定是臭名昭著的,但并不普遍。OP,如果没有人告诉过你,不要胡闹at包含那一行。它删除了所有内容。无论如何,isinstance()
不是很少使用的东西吗?这就是为什么Python使用基串来获取isinstance()
让路,这样你就可以继续你的生活,写一篇try/except.Thank:)我会根据你所说的来修复它。你能解释一下为什么会修复它吗?
a="test"
type(a)==type(user_input)