Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

i tried writing code to convert the temperature but my if statement is not working. Can someone help me why?

–2 votes
asked Feb 19, 2020 by anonymous
option1="conversion to celsius"
option2="conversion to fahrenheit"
display=input("Enter the option 1 or 2")
if display==1:
  temperature=float(input("Enter the temperature in fahrenheit"))
  #conversion into celsius
  fahrenheit=(temperature*9/5)+32
  print("temperature"+fahrenheit+"degree fahrenheit")
if display==2:
  temperature=float(input("Enter the temperature in celcius"))
  #conversion into celsius
  celsius=(temperature-32)*5/9
  print("temperature"+celsius+"degree celsius")
else:
  print("invalid")
quit()

1 Answer

0 votes
answered Feb 20, 2020 by catsanddo (440 points)
 
Best answer
It's a simple fix. The only problem is that input takes the users text as a string instead of an integer. To get your if statement to work I would suggest using this for line 4:

if display == '1':

and this for line 9:

elif display == '2':

You want to use elif in line 9 because otherwise you will always get an answer followed by invalid when picking option 1. Another fix to make would be to put the fahrenheit and celsius variables inside of str() when doing the final print() because otherwise you will get an error for adding a float to a string (the str() function converts your float to a string).
commented Feb 20, 2020 by anonymous
Thank you soo much.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...