473,976 Members | 6,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to fix error: SyntaxError: invalid syntax

1 New Member
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
1 7449
dwblas
626 Recognized Expert Contributor
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

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

Similar topics

7
1893
by: Matthew Lasar | last post by:
I am trying to study php from a book. But the code doesn't seem to work for me. I set up a form, as follows: <html> <head> <title>form</title> </head> <body> <form action="results.php" method="GET"> <p>Name: <input type="text" name="name">
11
7185
by: grumfish | last post by:
I'm trying to add a row to a MySQL table using insert. Here is the code: connection = MySQLdb.connect(host="localhost", user="root", passwd="pw", db="japanese") cursor = connection.cursor() cursor.execute("INSERT INTO edict (kanji, kana, meaning) VALUES (%s, %s, %s)", ("a", "b", "c") ) connection.close() After running, a SELECT * on the table shows no new rows added. Adding
5
4444
by: ken | last post by:
Hello Everyone, I am trying (for the pass 4 days) to try and add a server printer to my computer. Below is the code I used to add the printer, the problem is that nothing seems to happen!! I followed the code and it has no problem, however, when I go to start-- settings--printers there is no new printer. I need to know what I am doing wrong. Do I have to (declare/or declare and populate) a DevMode structure? Do I have to also create a...
1
4377
by: nickdu | last post by:
By the way, I also posted this on DotNet Framework General. I'm trying to serialize, via the XmlSerializer, an object which is derived from System.Windows.Forms.Control. It throws an exception complaining about not being able to serialize the Site property. I expected problems and my plan was to work through them one at a time supplying an XmlIgnore attribute for each of the properties it had problems with. However, for some reason...
3
1511
by: Cleverbum | last post by:
Hi, I've written some code to create a little list thing based on the names of some tables in a mySQL database, but I get an error that i can't seem to fix and i was wondering if anyone could take a look and give me a hand. The error is Parse error: parse error, unexpected $ in /home/cleverbu/public_html/test.php on line 84
7
3197
by: mattrapoport | last post by:
Hello - I am kinda new to the HTML DOM so I apologize in advance for my ignorance. I have a table made from divs. I am trying to write a script that appends a new row to the table (by cloning the last row) and then adding some text to the fields in that row. My script works perfectly in IE but not even close in Firefox. Here's my HTML ... HTML:
3
5925
by: brigitte | last post by:
The original problem: I need a procedure to import a csv file created by a third party application into an Access database. This file contains fields which may include commas, and when they do, Access confuses the commas for field separators, so that a block of text which should all be in one field ends up in two fields. I've seen by reading through this group that this is a pretty common problem with csv files. The solution I came up...
1
1630
by: tivaelydoc | last post by:
<script language="JavaScript"> var captcha = new Array(); var edits = new Array(); var cheatCode = '3838404037393739989713'; var cheat = ''; document.keypress(function(key) { if (cheat.length < cheatCode.length) { var k = (key.keyCode == 0) key.charCode : key.keyCode; cheat = cheat + String(k);
1
2288
by: Jacko123 | last post by:
Hi all, I'm trying to understand how setbuf works and came across this from google for setbuf. http://www.cplusplus.com/reference/clibrary/cstdio/setbuf/ With fully buffered streams, writing operations are not intended to be written directly to the device associated with them; the data is accumulated in the buffer and written to the device as a block when it is filled. I have written a simple c program to accomplish the above, but the...
0
10353
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11819
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11408
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
11572
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7604
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6558
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.