小编Nic*_*son的帖子

Python的type()函数及其"if"相关问题

所以我有以下代码:

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."
Run Code Online (Sandbox Code Playgroud)

不起作用 - 我相信因为if- 所以我把它改为:

if "str" in input_type
Run Code Online (Sandbox Code Playgroud)

"int"为浮点数和整数,但得到一个错误:

Traceback (most recent call last):
File …
Run Code Online (Sandbox Code Playgroud)

python if-statement function python-2.7

2
推荐指数
1
解决办法
3万
查看次数

标签 统计

function ×1

if-statement ×1

python ×1

python-2.7 ×1