【问题标题】:Python using input() within function to determine data typePython 在函数内使用 input() 来确定数据类型
【发布时间】:2017-10-28 03:25:02
【问题描述】:

我正在使用 Python 进行计算机科学导论的最后一章。有人可以告诉我我的代码有什么问题吗?结果只是BLANK

#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))

【问题讨论】:

  • 你没有使用用户输入的内容,你总是传入“0.23”。 input_type() 要么返回一个字符串,要么返回None,因此print() 将在调用时打印出多于空白的内容。如果你得到空白,你似乎没有在执行这个程序,因此在你的程序顶部添加一个临时的print("running ...."),以确保事情被调用/启动

标签: python function input edx


【解决方案1】:

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

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))

为我工作:)

【讨论】:

  • 你在 edX 上用过这个吗?
【解决方案2】:

这是您的错误。 当您运行程序时,它正在等待input()。你应该输入一些东西。所以这是持有整个程序。 您的程序的另一个问题。您已经硬编码了print(input_type("0.23")) 中的参数。所以无论你输入什么,它都是一样的。

编辑:另一个建议。请使用更好的逻辑来解决问题。只需考虑它并对其进行优化,您将在学习如何用任何语言编码方面走很长一段路。 :)

【讨论】:

  • 练习实际上应该提供一个代码,他们在我点击“提交”之前一直保留。我忘了删除 [userInput = input()]。 print() 只是用来测试我的代码是否有效。
【解决方案3】:

解决你的问题:

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"))

【讨论】:

  • 我得到了你的代码,但教程没有教我一些。我会审查这个。我尝试使用从教程中学到的知识,并且尽量不要超出我的知识范围,以免感到困惑。
猜你喜欢
  • 1970-01-01
  • 2020-02-27
  • 2018-04-14
  • 2019-06-12
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多