将 input() 识别为 str、float 或 int 并相应地采取行动 [英] Recognising an input() as either a str, float, or int and acting accordingly

查看:51
本文介绍了将 input() 识别为 str、float 或 int 并相应地采取行动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 Python 初学者.我正在编写一个使用无限循环的程序,并允许用户输入关键术语来访问不同的工具"或模块".

Python beginner here. I'm writing a program that uses an infinite loop and allows the user to enter key terms to access different 'tools' or 'modules'.

在这些模块"之一中,用户可以输入一个值并将其转换为二进制.我想:

Within one of these 'modules', the user can enter a value and convert it to binary. I want to:

  1. 允许程序识别值是 int 还是 afloat,然后运行将值转换为二进制的代码
  2. 允许程序识别输入的值是否为 str 并且 str 表示back",其中将退出当前循环.

据我所知,这个问题是在 input() 自动将输入的任何内容转换为 str 时发生的(由于:http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html "首先它打印作为参数给出的字符串").

As far as I know this issue is occurring as input() converts whatever is entered to a str automatically (due to: http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/io.html "First it prints the string you give as a parameter").

如何让下面的代码识别输入是 str、float 还是 int,然后执行相关的 if 语句?目前,我的这部分代码可以接受 'back' 退出循环,但会将任何 int 或 float 值作为 str ,使程序提示用户再次输入十进制值.

How can I make the code below recognize if the input is a str, float, or int and then execute the relevant if statements? Currently, this part of my code can accept 'back' to exit out of the loop but will take any int or float value as a str, making the program prompt the user to enter a decimal value once more.

    #decimal to binary   
    while search == "d2b":
        dec2bin = input("\nDecimal Value: ")
        if type(dec2bin) == int:
            print("Binary Value: " + "{0:b}".format(dec2bin))
        elif type (dec2bin) == str:
            if dec2bin == "back":
                search = 0
        elif type (dec2bin) == float:
                #code for float to binary goes here

与此线程不同(Python:分析输入以查看它是整数、浮点数还是字符串),因为在 input() 上使用列表E2:似乎不能使用建议的重复作为问题的解决方案.但是,Francisco 在此线程中的评论有解决方案

not the same as this thread (Python: Analyzing input to see if its an integer, float, or string), as a list is used there over input() E2: cannot seem to use suggested duplicate as a solution to issue. However, a comment in this thread by Francisco has the solution

推荐答案

使用异常!intfloat 函数在无法转换传递的值时会抛出 ValueError 异常.

Use exceptions! The int and float functions throw a ValueError exception when they can't convert the value passed.

while search == "d2b":
    dec2bin = input("\nDecimal Value: ")
    try:
        dec2bin = int(dec2bin)
    except ValueError:
        pass
    else:
        print("Binary Value: " + "{0:b}".format(dec2bin))
        continue

    try:
        dec2bin = float(dec2bin)
    except ValueError:
        pass
    else:
        #code for float to binary goes here
        continue

    if dec2bin == "back":
        search = 0

尝试转换的顺序很重要,因为传递给 int 的每个值都对 float 有效,并且每个值传递给 float 是一个有效的传递给 str

The order in which you try the conversions is important, since every value passed to int is valid with float, and every value passed to float is a valid to be passed to str

这篇关于将 input() 识别为 str、float 或 int 并相应地采取行动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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