【问题标题】:How can I make this work using a nested if-else when detecting the type of data of the inputted data using python使用python检测输入数据的数据类型时,如何使用嵌套的if-else进行这项工作
【发布时间】:2021-02-14 23:46:38
【问题描述】:

我想知道在使用if else语句并检测输入的数据是哪种数据类型时如何使python程序工作。

这是我根据我的知识输入的代码(对不起,我是新手):

FullName = input("Enter your Full Name: ")
data = input("Enter a data: ")
if type(eval(data)) == float and int:
    if type(eval(data)) == float:
        print("Hello,", FullName + ". Your inputted data", data, "is a type of Float!")
    else:
        print("Hello,", FullName + ". Your inputted data", data, "is a type of Integer!")
else:
    print("Hello,", FullName + ". Your inputted data", data, "is a type of String!")

如果您能纠正我的工作,请随意,我将不胜感激。谢谢!

【问题讨论】:

    标签: python-3.x types nested-if


    【解决方案1】:
    1. 你可能指的是float or int而不是float and int
    2. 你不能使用和/或那种方式你需要说 type(eval(data)) == float or type(eval(data)) == int 否则你会得到奇怪的行为,因为 or 运算符将 something == something(这是一个布尔值)与另一个不是布尔值的类型进行比较。
    3. 如果您使用 elif,则此处不需要嵌套 if 语句 (如果 int ... elif float ... else )

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 2020-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多