0

I am a beginner and it might feel like a silly question.

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

Which takes:

  1. 'quit' for stopping program
  2. int values for providing the correct result

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.

Here is my program:

# 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)
Adalcar
1,46812 silver badges27 bronze badges
asked Sep 14, 2016 at 18:15
4
  • 1
    user input is naturally a string, try casting to an int? Commented Sep 14, 2016 at 18:19
  • Can't reproduce, it works for me. I would recommend just using continue in the except clause. Commented Sep 14, 2016 at 18:21
  • 1
    In Python 3.x input returns a str, in Python 2.x input returns an int Commented Sep 14, 2016 at 18:22
  • Can you enter two times ' temperature' instead of an integer into this program? Commented Sep 14, 2016 at 18:27

5 Answers 5

1

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

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

Code (Check comments):

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')

Output:

 C:\Users\dinesh_pundkar\Desktop>python c.py
 1 Enter the temperature in celsius: Enter Quit if you want to close the conve
rter"57"
Type is = <type 'str'>
('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 = <type 'str'>
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 = <type 'str'>
('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>

Also, if you are using Python 2, then use raw_input instead of input. For Python 3 input works.

answered Sep 14, 2016 at 18:27
Sign up to request clarification or add additional context in comments.

5 Comments

Of the flurry of answers to this question, only this one makes any sense. Crazy! :P
Thanks for the the help Dinesh. I know I unnecessarily wrote 3 loops but my question. Your answer was perfect from a learning perspective. But only question i have is hile type(userinput)==str: userinput = (input(" 2. Oops Enter the temperature in celsius: " "Enter Quit if you want to close the converter")) print(type(userinput)) why does a integer entered is showing us string type?
@begineer input will always be a string, and there's no need to have a nested while loop. Let the main loop fail and it just starts again. You cannot test for whether or not a temperature for in Celsius, it's an unbound scale (except for absolute zero)
@begineer - roganjosh is correct that input will always string. I have updated my code. It will give you example of what roganjosh has explained. Check the line - Type is in output. It will give type of userinput.
@begineer - Please let me know if you have any other query.
0

Not sure if I understand the problem correctly, but usually it is better to use raw_input instead of input in case like this.

answered Sep 14, 2016 at 18:21

3 Comments

Unless they use Python 3, in which case raw_input doesn't exist
Right, was too quick to judge. Main problem was the second while :)
I agree. Jhutar
0

You are using three nested while loops here for no obvious resons. When the user enters a wrong value, he gets transferred to the next while loop which never ends. This is a quick and dirty solution.

# 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")
 try:
 userinput= float(userinput)
 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)
 except ValueError:
 print('You cannot enter text, please use numbers') 
 if userinput.lower()=='quit':
 print('Closing the application. Thanks for using the converter')
 break
answered Sep 14, 2016 at 18:25

Comments

0

the problem is here:

if type(userinput)!=str:

since input returns a string, userinput is always a string: Thus, the above snippet is always false. you should try parsing it as a float like in this answer

answered Sep 14, 2016 at 18:26

Comments

0

In the 1 input, you cast it, in the second one, you never cast.

I also noticed, you never actually implemented the "quit" functionality for that loop. This should make your code a bit easier to read.

def checkToClose(value):
 if (value.lower() == 'quit')
 quit()
while True:
 userinput= input(" 1 Enter the temperature in celsius: "
 "Enter Quit if you want to close the converter")
 checkToClose(userinput)
 try:
 userinput= float(userinput)
 catch:
 continue
 f = userinput * 9.0 / 5 + 32
 print('Your input was:',userinput,'\n'*2,'The corresponding Fareinheit is',f,'\n'*3, '---'*25)
answered Sep 14, 2016 at 18:29

1 Comment

Fixing the syntax errors in this and accounting for Python 2/3 compatibility issues, this doesn't work. There is no catch in Python, it's except but even then, why would you continue into a failing calculation?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.