By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,345 Members | 1,746 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,345 IT Pros & Developers. It's quick & easy.

How to fix error: SyntaxError: invalid syntax

P: 1
Hey, I just picked up python again after not coding with it for many years. But I wanted to throw some simple scripts together to see if I remember all the functionality so far. Now I've got this code:

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2.  
  3. # A program developed to see if the temperature is Celcius or Farhenheit
  4. # and than convert it to the other in a more user friendly manner
  5.  
  6. import os, sys
  7. import math
  8.  
  9. def far(temp):
  10.  
  11.     Celcius = ((temp - 32) * 5) / 9
  12.     Kelvin = Celcius + 273.15
  13.  
  14. def cel(temp):
  15.  
  16.     Far = (temp * 1.8) + 32
  17.     Kelvin = temp + 273.15
  18.  
  19. def kelvin(temp):
  20.  
  21.     Celcius = temp + 273.15
  22.     Far = (Celcius * 1.8) + 32
  23.  
  24.  
  25. human = raw_input("Is your temperature in Fahrenheit (f), Celcius (c), or Kelvin (k) --> ")
  26. temp1 = int(raw_input("What is your temperature --> ")
  27.  
  28. if human=="f":
  29.     far(temp1)
  30.     print "Your temperature is %s in Celcius and %s in Kelvin" % Celcius,Kelvin
  31. if human=="c":
  32.     cel(temp1)
  33.     print "Your temperature is %s in Fahrenheit and %s in Kelvin" % Far,Kelvin
  34. if human=="k":
  35.     kelvin(temp1)
  36.     print "Your temperature is %s in Celcius and %s in Fahrenheit" % Celcius,Far
  37.  
I want the user to define which kind of temperature they have and than the program to output the other temperatures. But so far all I get is an error in the syntax at line 28:

Expand|Select|Wrap|Line Numbers
  1.   File "temp2.py", line 28
  2.     if human=="f":
  3.                  ^
  4. SyntaxError: invalid syntax
  5.  
Anyone have any thoughts?
Nov 5 '10 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 621
You have to always check the previous line as well. If you have forgotten a closing parenthesis for example, the interpreter will think that this line is a continuation of the previous line and point to this line with the error. Also consider wrapping the input in a try/except, so if someone enters "F" or "98.6" for the temperature instead of an integer, you can catch it and then ask for a whole number.
Expand|Select|Wrap|Line Numbers
  1. ##   depending on how much input checking you want to do
  2. human = ""
  3. while human.lower() not in ["c", "f", "k"]:
  4.     human = raw_input("Is your temperature in Fahrenheit (f), Celsius (c), or Kelvin (k) --> ")
  5.  
  6. ## simplified example
  7. try:
  8.     temp1 = int(raw_input("What is your temperature --> "))
  9. except:
  10.     print "The temperature must be a whole number" 
Nov 5 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.