Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
286 views
in Technique[技术] by (71.8m points)

python - Recognising an input() as either a str, float, or int and acting accordingly

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. Allow the program to recognize if the value is either an int or a float and then run code that converts the value to binary
  2. Allow the program to recognize if the value entered is a str and the str says 'back', in which the current loop will be exited.

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

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("
Decimal 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

Edit: 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

while search == "d2b":
    dec2bin = input("
Decimal 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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...