Convert strings to int or float in Python 3?

后端 未结 5 931
无人及你
无人及你 2020-12-11 16:13
 integer = input(\"Number: \")
 rslt = int(integer)+2
 print(\'2 + \' + integer + \' = \' + rslt)
 double = input(\"Point Number: \")
 print(\'2.5 + \' +double+\' =          


        
相关标签:
5条回答
  • 2020-12-11 16:47
    integer = int(input("Number: "))
    print('2 + %d = %d' % (integer, integer + 2))
    
    double = float(input("Point Number: "))
    print('2.5 + %.2f = %.2f' % (double, double + 2.5))
    
    0 讨论(0)
  • 2020-12-11 16:54

    In Python 3.x - input is the equivalent of Python 2.x's raw_input...

    You should be using string formatting for this - and perform some error checking:

    try:
        integer = int(input('something: '))
        print('2 + {} = {}'.format(integer, integer + 2))
    except ValueError as e:
        print("ooops - you didn't enter something I could make an int of...")
    

    Another option - that looks a bit convoluted is to allow the interpreter to take its best guess at the value, then raise something that isn't int or float:

    from ast import literal_eval
    
    try:
        value = literal_eval(input('test: '))
        if not isinstance(value, (int, float)):
            raise ValueError
        print value + 2
    except ValueError as e:
        print('oooops - not int or float')
    

    This allows a bit more flexibility if you wanted complex numbers or lists or tuples as input for instance...

    0 讨论(0)
  • 2020-12-11 16:55

    You have to convert the integer into a string:

    print('2 + ' + str(integer) + ' = ' + str(rslt))
    

    Or pass it as an argument to print and print will do it for you:

    print('2 +', integer, '=', rslt)
    

    I would do it using string formatting:

    print('2 + {} = {}'.format(integer, rslt))
    
    0 讨论(0)
  • 2020-12-11 17:02

    If you want to convert a value to an integer, use the int built in function, and to convert a value to a floating point number, use the float built in function. Then you can use the str built in function to convert those values back to strings. The built in function input returns strings, so you would use these functions in code like this:

    integer = input("Number: ")
    rslt = int(integer)+2
    print('2 + ' + integer + ' = ' + str(rslt))
    double = input("Point Number: ")
    print('2.5 + ' +str(double)+' = ' +str(float(double)+2.5)
    
    0 讨论(0)
  • 2020-12-11 17:03

    Your problem is not with converting the input to an integer. The problem is that when you write ' = ' + rslt you are trying to add an integer to a string, and you can't do that.

    You have a few options. You can convert integer and rslt back into strings to add them to the rest of your string:

    print('2 + ' + str(integer) + ' = ' + str(rslt))
    

    Or you could just print multiple things:

    print('2 + ', integer, ' = ', rslt)
    

    Or use string formatting:

    print('2 + {0} = {1}'.format(integer, rslt))
    
    0 讨论(0)
提交回复
热议问题