-2

I'm trying to set up a 'Fahrenheit to Celsius' and 'Celsius to Fahrenheit' calculator that would allow the user to select if they want Celsius converted to Fahrenheit or visa versa but I'm getting an invalid syntax error and I'm not quite sure why.The code I'm using// The 'Invalid Syntax Error' I understand the problem could be pretty basic, I'm relatively new to python just.

CelsiusorFarenheit = input("Would you like Celsius to Farenheit or Farenheit
to Celcius?")
if CelsiusorFarenheit == "celsius" or "Celsius":
 def Celsius():
  centToFaren = int(input("Enter a temperature in Celsius: "))
  Farenheit= 9.0/5.0 * centToFaren+32
  print ("Temperature:", centToFaren, "Celsius =", Farenheit, "F")
else CelsiusorFarenheit == "farenheit" or "Farenheit":
 def Farenheit():
  farenToCent = int(Input("Enter a temperature in Farenheit: "))
  Celcius=(farenToCent-32)*5.0/9.0
  print("Temperature:", centToFaren, "Fahrenheit =", Celcius, "C")

2 Answers2

4

There are a few points i notice from the above code.

To resolve the syntax error, you will need to update the else to an elif followed by the condition.

To improve the code 2 points further, you can call lower() on your variable as below

CelsiusorFarenheit.lower()

and update the comparisons to long form as below.

def celsius():
    centToFaren = int(input("Enter a temperature in Celsius: "))
    Farenheit= 9.0/5.0 * centToFaren+32
    print ("Temperature:", centToFaren, "Celsius =", Farenheit, "F")

def farenheit():
    farenToCent = int(Input("Enter a temperature in Farenheit: "))
    Celcius=(farenToCent-32)*5.0/9.0
    print("Temperature:", centToFaren, "Fahrenheit =", Celcius, "C")

if CelsiusorFarenheit.lower() == "celsius":
    celsius()
elif CelsiusorFarenheit.lower() == "farenheit":
    farenheit()
Gragas
  • 84
  • 2
  • Ahh I see, I appreciate the added pointers! This has fixed the issue I had now I just need it to print both output values in two decimal values. – jackrussel83 Nov 07 '18 at 20:42
-3

You are putting a condition in an else block, which is a syntax error. else blocks are executed when no other condition matches so they cannot have conditions. You can change it to an elif statement or remove the condition. So change this line:

else CelsiusorFarenheit == "farenheit" or "Farenheit":

to either this:

elif CelsiusorFarenheit.lower() == "farenheit":

or this:

else:
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42