0

I decided to learn Python over quarantine and I started playing around with inputs. I wanted to make a calculator that would change Fahrenheit to Celsius. Everything works, except it always prints out the result of No, even if I type Yes. Here is the code for reference:

def Fahrenheit_to_Celsius():
    Fahrenheit = float (input("What temperature (in Fahrenheit) would you like to convert into 
    Celsius?"))
    Celsius = (Fahrenheit - 32) * 5/9
    print (Fahrenheit, "F is", Celsius, "C")
    rounded_celsius = str (input("Would you like your Celsius degree rounded? Type 'Yes' or 'No'."))
    if rounded_celsius == "NO" or "no" or "No":
        print ("Ok, have a nice day!") #Always prints this, even if I put Yes
    elif rounded_celsius == "Yes" or "YES" or "yes":
        print (round(Celsius))
    else:
        print ("Ok, have a nice day!")
Fahrenheit_to_Celsius()

What can I do to make sure that the function prints out the result of Yes if the user types Yes? I am still a beginner, so every answer is appreciated!

StormHawk
  • 37
  • 5

2 Answers2

1

You can add .lower() to the rounded_celsius input so you can compare it to 'yes' or 'no' rather than multiple variations of the same word. Then just update the if and elif to check for the lowercase 'yes' or 'no'. Your code doesn't actually hit the no if statement! It just looks like it because the print is the same as the else.

def Fahrenheit_to_Celsius():
    Fahrenheit = float (input("What temperature (in Fahrenheit) would you like to convert into Celsius?"))
    Celsius = (Fahrenheit - 32) * 5/9
    print (Fahrenheit, "F is", Celsius, "C")
    rounded_celsius = str (input("Would you like your Celsius degree rounded? Type 'Yes' or 'No'.")).lower()
    if rounded_celsius == "no":
        print ("Ok, have a nice day!") #Always prints this, even if I put Yes
    elif rounded_celsius == "yes":
        print (round(Celsius))
    else:
        print ("Ok, have a nice day!")
Fahrenheit_to_Celsius()
Lemon.py
  • 819
  • 10
  • 22
1

Try using in instead of == for conditional checks in if..else loop

def Fahrenheit_to_Celsius():
    Fahrenheit = float (input("What temperature (in Fahrenheit) would you like to convert into Celsius?"))
    Celsius = (Fahrenheit - 32) * 5/9
    print (Fahrenheit, "F is", Celsius, "C")
    rounded_celsius = str (input("Would you like your Celsius degree rounded? Type 'Yes' or 'No'."))
    if (rounded_celsius in ["No","no","NO","nO"]):
        print ("Ok, have a nice day!") #Always prints this, even if I put Yes
    elif (rounded_celsius in ["Yes",'yes','yEs','YES','yeS']):
        print (round(Celsius))
    else:
        print ("Ok, have a nice day!")
Fahrenheit_to_Celsius()
Vinay Kurmi
  • 77
  • 10