【问题标题】:How to prrint the Data Type in Python?如何在 Python 中打印数据类型?
【发布时间】:2020-07-07 14:57:30
【问题描述】:

我是 Python 新手。我编写了一个简单的代码来了解输入的数据类型。 这是我的代码。 我提供了 2 个输入,它们被转换为“字符串”。使用“If”条件匹配输入数据类型。但是我的输出很奇怪。我不知道为什么它在这里打印唯一的整数。谁能帮我解决这个问题?

我也在这里添加了我的输出

代码:

a=str(input("Enter A Value \n"))
b=str(input("Enter B value \n"))
print('\n')
print('A is = ',a)
print('B is = ',b)

if (type(a)==int, type(b)==int):
print('A and B is Integer')

elif (a==str, b==str):
    print('A and B is String')

elif (a==float, b==float):
    print('\nA and B is Float')

print('\n*Program End*')'

输出

Enter A Value 
abc
Enter B value 
def


A is =  abc
B is =  def

A and B is Integer

*Program End*

【问题讨论】:

  • 鉴于: 1. input 给你一个字符串; 2.然后你显式地将它传递给str,为什么不清楚ab的数据类型是什么?
  • 这能回答你的问题吗? How to determine a Python variable's type?
  • @jonrsharpe 和所有的比较只是测试一个非空元组的真实性.....
  • 您可能想print((type(a)==int, type(b)==int))(注意所需的双括号)查看您实际检查的内容。另请注意,具有至少一个元素的容器被认为是真的。
  • stackoverflow.com/users/5349916/mistermiyagi - 我按照你的方式试过了。但是,我的输出仍然是相同的“A 和 B 是整数”我的问题是:我知道我在那里给出了字符串值。但我想通过“elif”语句进行匹配,我希望在那里得到“A and B as String”的输出。

标签: python


【解决方案1】:

(我不确定这是否是您要查找的内容)您可以使用 type() 函数在 python 中打印某些数据类型,如下所示:

var = 25
print(type(var))

输出:

<class 'int'>

【讨论】:

    【解决方案2】:

    问题出在这一行:

    if (type(a)==int, type(b)==int)
    

    ^ 这适用于您拥有的所有if 条件。

    这不是在if 语句上使用多个条件的方法。事实上,这只是一个tuple。所以你说if (0,0)。所以。根据documentation

    默认情况下,一个对象被认为是真的,除非它的类定义 返回 False 的 bool() 方法或 len() 方法 使用对象调用时返回零。 1 这里是大部分 内置对象被认为是错误的:

    - constants defined to be false: None and False.
    - zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
    - empty sequences and collections: '', (), [], {}, set(), range(0)
    

    在这种情况下,您将tuplelen() != 0 一起使用,因此在检查其真值时它将始终返回True。所以检查多个真值条件的正确方法是使用boolean operators

    a=str(input("Enter A Value \n"))
    b=str(input("Enter B value \n"))
    print('\n')
    print('A is = ',a)
    print('B is = ',b)
    
    if type(a)==int and type(b)==int:
        print('A and B is Integer')
    elif type(a)==str and type(b)==str:
        print('A and B is String')
    elif type(a)==float and type(b)==float:
        print('\nA and B is Float')
    
    print('\n*Program End*')
    

    ^ 请注意,我将type() 添加到其他条件中,因为它们不存在。

    现在。这里还有一个问题:

    a=str(input("Enter A Value \n"))
    b=str(input("Enter B value \n"))
    

    您正在将输入转换为str,这已经是str,因为input 给您str,所以您将始终得到:

    A and B is String

    因为他们都是str。因此,您可以为此使用 str 内置函数:

    if a.isnumeric() and b.isnumeric():
        print('A and B is Integer')
    elif a.isalpha() and b.isalpha():
        print('A and B is String')
    elif a.replace('.','',1).isdigit() and b.replace('.','',1).isdigit():
        print('\nA and B is Float')
    

    第一个是a.isnumeric(),然后是a.alpha(),最后一个解决方法是检查它是否是float:将. 替换为1,然后检查它是否仍然是isdigit()

    【讨论】:

      【解决方案3】:

      type() 方法将返回类类型

      a=5
      print(a,"is type of",type(a))
      b=2.9
      print(b,"is type of",type(b))
      c='Hello'
      print(c,"is type of",type(c))
      

      输出:

      5 is type of <class 'int'>
      2.9 is type of <class 'float'>
      Hello is type of <class 'str'>
      

      【讨论】:

      • 我想将数据类型与我的输入相匹配。我给了字符串作为输入。当我使用“elif”语句时,我正在检查我的输入数据类型并期望输出只是“字符串”。但它以“整数”形式给出,程序结束。
      【解决方案4】:
      integer=100
      print(integer,"is a",type(integer))
      string="Python"
      print(string,"is a",type(string))
      decimal=5.973 #decimal is also called  as float.
      print(decimal,"is a",type(decimal))
      

      输出:

      100 is a <class 'int'>
      Python is a <class 'str'>
      5.973 is a <class 'float'>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-17
        • 2015-12-09
        • 1970-01-01
        • 2022-01-14
        • 2023-03-26
        相关资源
        最近更新 更多