Python在函数中使用input()确定数据类型

Python在函数中使用input()确定数据类型,python,function,input,edx,Python,Function,Input,Edx,我在介绍使用Python的计算机科学的最后一章中。有人能告诉我我的代码有什么问题吗?结果只是空白 #Write a function called "input_type" that gets user input and #determines what kind of string the user entered. # - Your function should return "integer" if the string only # contains character

我在介绍使用Python的计算机科学的最后一章中。有人能告诉我我的代码有什么问题吗?结果只是空白

#Write a function  called "input_type" that gets user input and 
#determines what kind of string the user entered.

#  - Your function should return "integer" if the string only
#    contains characters 0-9.
#  - Your function should return "float" if the string only
#    contains the numbers 0-9 and at most one period.
#  - You should return "boolean" if the user enters "True" or
#    "False". 
#  - Otherwise, you should return "string".

#Remember, start the input_type() function by getting the user's
#input using the input() function. The call to input() should be
#*inside the* input_type() function.


def input_type(userInput):
    digitTable = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    test1 = userInput.find(digitTable)
    if userInput == "True" or userInput == "False":
        return "boolean"
    elif test1 == -1:  # No digits
        return "string"
    elif userInput == "True" or userInput == "False":
        return "boolean"
    else:  # Contains digits
        test2 = userInput.find(".") # find decimal
        if test2 == -1:  # No decimals means it is an integer
            return "integer"
        else:  # Yes if float
            return "float"

userInput = input()
print(input_type(userInput))

要改进用户的代码并使其更短更好,可以执行以下操作:

import re

def input_type(userInput):
    if userInput in ("True", "False"):
        return "boolean"
    elif re.match("^\d+?\.\d+?$", userInput):
        return "float"
    elif userInput.isdigit():
        return "int"
    else:
        return "string"

res = input()
print(input_type(res))
适用于我:)

这是您的错误。 运行程序时,它正在等待
输入()。你应该进入一些东西。这就是整个计划。
你的程序还有一个问题。您已经在
打印(输入类型(“0.23”)
中对参数进行了硬编码。所以不管你输入什么,它都是一样的

编辑:另一个建议。请用更好的逻辑来解决这个问题。只要仔细考虑并优化它,您就需要花很长的时间学习如何用任何语言编写代码。:)

要解决您的问题:

def input_type(userInput):
    digitTable = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    test1 = userInput.find(''.join([str(x) for x in digitTable]))
    if userInput == "True" or userInput == "False":
        return "boolean"
    elif test1 == -1:  # No digits
        return "string"
    elif userInput == "True" or userInput == "False":
        return "boolean"
    else:  # Contains digits
        test2 = userInput.find(".") # find decimal
        if test2 == -1:  # No decimals means it is an integer
            return "integer"
        else:  # Yes if float
            return "float"

userInput = input()
print(input_type("0.23"))

您没有使用用户键入的内容,您总是传入“0.23”
input_type()
返回字符串或
None
,因此
print()
在调用时将打印大于空白的内容。如果为空,您似乎不会执行此程序,因此请在程序顶部添加一个临时的
打印(“running…””)
,以确保调用/启动事件。此练习实际上应该为该t提供一个代码,直到我点击“submit”为止。我忘记删除[userInput=input()]。print()只是用来测试我的代码是否有效。我得到了你的代码,但教程没有教我这些。我来复习一下。我试着使用我从教程中学到的知识,尽量不要超出我的知识范围,这样我就不会感到困惑。你们在edX上用过这个吗?