Reputation: 11
I have a simple temperature converter that takes user input in celsius and converts to Fahrenheit. 1) This is stored in a variable named tempcel. 2) Check if the tempcel has numbers in it by using the isdigit method. 3) Then the conversion formula is applied and answer is stored in another variable called tempfar. This is then printed.
However I noticed that i cannot input negative celsius temperatures in this - they are being treated as string and hence being prompted to enter again!
Trying to see how i can work this so that it should prevent users from entereing text and also take into account that someone can enter negative numbers. Can someone help? The code is given below . thanks..
tempcel = (input("please enter a temperature in Celcius : "))
while True:
if (tempcel.isdigit()):
tempcel = float(tempcel)
tempfar = float((tempcel*9)/5 + 32)
print("The temperature is ",tempfar, "degrees Fahrenheit.")
if tempfar<=32:
print("Whoa! Its freezing cold today!")
elif tempfar<=50:
print("It is chilly today!")
elif tempfar<=90:
print("It is OK")
else:
print("Goddamn! Its hot today")
break
else:
print("Sorry you cannot enter a string of characters here. Please try again")
tempcel = (input("please enter a temperature in Celcius :" ))
Upvotes: 0
Views: 649
Reputation: 46779
The best approach is to add exception handling to catch if the conversion to a float
fails. Also, there is no need to have two input()
calls, it could be simplified as follows:
while True:
tempcel = (input("please enter a temperature in Celcius : "))
try:
tempcel = float(tempcel)
tempfar = (tempcel * 9.0) / 5.0 + 32.0
print("The temperature is {:.1f} degrees Fahrenheit.".format(tempfar))
if tempfar <= 32:
print("Whoa! Its freezing cold today!")
elif tempfar <= 50:
print("It is chilly today!")
elif tempfar <= 90:
print("It is OK")
else:
print("Goddamn! Its hot today")
break
except ValueError:
print("Sorry you cannot enter a string of characters here. Please try again")
Also, there is no need to convert to float
twice if you use floats in your calculation. The output can be be restricted to 1 decimal place by using format, e.g. {:.1f}
.
Upvotes: 0
Reputation: 17322
you could change the way you test if the string is a valid number :
check_string(input_string):
try:
return float(input_string)
except ValueError:
return None
Upvotes: 0
Reputation: 2939
Your code in this case would also be vulnerable to failing if people enter 30.5
degrees which may not be desirable. A different way of handling this may be to use a try:... except:...
clause as follows:
tempcel = (input("please enter a temperature in Celcius : "))
while True:
try:
tempcel = float(tempcel)
tempfar = float((tempcel*9)/5 + 32)
# the rest of the code you had before...
except ValueError:
print("Sorry you cannot enter a string of characters here. Please try again")
tempcel = (input("please enter a temperature in Celcius :" ))
This means that python will attempt to convert the input to a float but if it fails with a ValueError (unable to make the conversion), it will prompt the user again.
Upvotes: 1