如何检查输入是浮点数还是整数? [英] How to check if input is float or int?

查看:55
本文介绍了如何检查输入是浮点数还是整数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个简单的转换器,打印浮点数或整数的十六进制数.我的代码是:

number = input("请输入您的号码...... \n")如果类型(数字)==类型(2.2):print("输入的数字是浮点数,十六进制数是:", float.hex(number))elif 类型(数字)== 类型(2):print("输入的数字是,",number,"并且它的十六进制数字是:",hex(number))别的:print("您输入的号码无效")

但它总是跳过前两个语句,只打印 else 语句.有人可以找出问题吗?

解决方案

TLDR:首先使用 ast.literal_eval 转换您的 input.


Python3中input的返回类型总是str.因此不能根据 type(2.2)(又名 float)和 type(2)(又名 int)检查它成功.

<预><代码>>>>数字 = 输入()3>>>编号,类型(编号)('3', )

最简单的方法是明确要求 Python 转换您的输入.ast.literal_eval 允许保存转换为 Python 的基本数据类型.它会自动将 intfloat 文字解析为正确的类型.

<预><代码>>>>进口AST>>>数字 = ast.literal_eval(input())3>>>编号,类型(编号)(3, <class 'int'>)

在您的原始代码中,将 ast.literal_eval 应用于用户输入.然后您的类型检查可以成功:

导入 astnumber = ast.literal_eval(input("请输入您的号码......\n"))如果类型(数字)是浮点数:print("输入的数字是浮点数,十六进制数是:", float.hex(number))elif 类型(数字)是整数:打印(输入的数字是,",数字,它的十六进制数字是:",十六进制(数字))别的:打印(您输入的类型无效")


急切地尝试转换输入也意味着您的程序可能会收到无效的输入,就转换器而言.在这种情况下,不是获取另一种类型的 some 值,而是会引发异常.使用try-except来应对这种情况:

导入 ast尝试:number = ast.literal_eval(input("请输入您的号码......\n"))除了作为错误的异常:打印(您的输入无法解析:",错误)别的:如果类型(数字)是浮点数:print("输入的数字是浮点数,十六进制数是:", float.hex(number))elif 类型(数字)是整数:打印(输入的数字是,",数字,它的十六进制数字是:",十六进制(数字))别的:打印(您输入的类型无效")

I want to make a simple converter, to print either hexadecimal number of float or integer. My code is:

number = input("Please input your number...... \n") 
if type(number) == type(2.2):
    print("Entered number is float and it's hexadecimal number is:", float.hex(number)) 
elif type(number) == type(2):
    print("Entered number is, ", number,"and it's hexadecimal number is:", hex(number)) 
else:
    print("you entered an invalid number")

But it is always skipping the first two statements and just print else statements. Can someone please find out the problem ?

解决方案

TLDR: Convert your input using ast.literal_eval first.


The return type of input in Python3 is always str. Checking it against type(2.2) (aka float) and type(2) (aka int) thus cannot succeed.

>>> number = input()
3
>>> number, type(number)
('3', <class 'str'>)

The simplest approach is to explicitly ask Python to convert your input. ast.literal_eval allows for save conversion to Python's basic data types. It automatically parses int and float literals to the correct type.

>>> import ast
>>> number = ast.literal_eval(input())
3
>>> number, type(number)
(3, <class 'int'>)

In your original code, apply ast.literal_eval to the user input. Then your type checks can succeed:

import ast

number = ast.literal_eval(input("Please input your number...... \n"))
if type(number) is float:
    print("Entered number is float and it's hexadecimal number is:", float.hex(number)) 
elif type(number) is int:
    print("Entered number is, ", number,"and it's hexadecimal number is:", hex(number)) 
else:
    print("you entered an invalid type")


Eagerly attempting to convert the input also means that your program might receive input that is not valid, as far as the converter is concerned. In this case, instead of getting some value of another type, an exception will be raised. Use try-except to respond to this case:

import ast

try:
    number = ast.literal_eval(input("Please input your number...... \n"))
except Exception as err:
    print("Your input cannot be parsed:", err)
else:
    if type(number) is float:
        print("Entered number is float and it's hexadecimal number is:", float.hex(number)) 
    elif type(number) is int:
        print("Entered number is, ", number, "and it's hexadecimal number is:", hex(number)) 
    else:
        print("you entered an invalid type")

这篇关于如何检查输入是浮点数还是整数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆