Python during runtime does not consider a number entered as a int or float
<p>I am a beginner and it might feel like a silly question. </p>

<p>I am trying to create a simple program,a  temperature converter from  Celsius to F*.</p>

<p>Which takes:</p>

<ol>
<li>'quit' for stopping program</li>
<li>int values  for providing the correct result</li>
</ol>

<p>Problem: any text value entered by user is handled through try expect error and a while loop which ask them to enter the int value again, but the second time user enters a correct value (which is int) the system considers it as a string and the loop is never ending.</p>

<p>Here is my program:</p>

<pre><code># Program to convert Celsius to Fahrenheit

import time

while True:

    userinput= input(" 1 Enter the temperature in celsius: "
                 "Enter Quit if you want to close the converter")

    if userinput.lower()=='quit':
        print('Closing the application. Thanks for using the converter')
        break

    try:
        userinput= float(userinput)

    except ValueError:
        print('You cannot enter text, please use numbers')
        while userinput.lower()!='quit':

            if type(userinput)!=str:
                userinput = float(userinput)
                print(type(userinput))

                break
            else:
                while type(userinput)==str:

                    userinput = (input(" 2. Oops Enter the temperature in celsius: "
                              "Enter Quit if you want to close the converter"))
                    print(type(userinput))
                    ## my problem is here even if enter a integer 2 as userinput. i  am getting the type userinput as str. Can someone help me

    type(userinput)
    userinput=float(userinput)
    f=userinput*9.0/5+32
    print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)
</code></pre>

<p>Code after <strong><em>print('You cannot enter text, please use numbers')</em></strong> is not required. Due this code, it is stucked in infinite loop once you enter wrong input</p>

<p>Since input is asked in while loop, user input will be asked though the wrong input is given.</p>

<p><strong>Code (<em>Check comments</em>):</strong></p>

<pre><code>while True:
#Get input
userinput= input(" 1 Enter the temperature in celsius: "
                 "  Enter Quit if you want to close the converter")
#Check user wants to quit
if userinput.lower()=='quit':
    print('Closing the application. Thanks for using the converter')
    break

try:
    #Check type of userinput
    print "Type is =",type(userinput)
    #Check user input is number
    userinput= float(userinput)
    f=userinput*9.0/5+32
    print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)

except ValueError:
    #If user input is not number, then print error message.
    #After this, again user input will be asked due to while loop.
    print('You cannot enter text, please use numbers')
</code></pre>

<p><strong>Output:</strong></p>

<pre><code>  C:\Users\dinesh_pundkar\Desktop&gt;python c.py
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"57"
Type is = &lt;type 'str'&gt;
('Your input was:', 57.0, '\n\n', 'The corresponding Fareinheit is', 134.6, '\n\
n\n', '-------------------------------------------------------------------------
--')
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"asd"
Type is = &lt;type 'str'&gt;
You cannot enter text, please use numbers
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"34"
Type is = &lt;type 'str'&gt;
('Your input was:', 34.0, '\n\n', 'The corresponding Fareinheit is', 93.2, '\n\n
\n', '--------------------------------------------------------------------------
-')
 1 Enter the temperature in celsius:   Enter Quit if you want to close the conve
rter"quit"
Closing the application. Thanks for using the converter

C:\Users\dinesh_pundkar\Desktop&gt;
</code></pre>

<p><em>Also, if you are using Python 2, then use raw_input instead of input. For Python 3 input works.</em></p>

python input