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

Else error

P: 5
Python Version 2.0
Windows Vista (I know, I know...)

Im very new to python and have only been doing it for two weeks.


print 'Type in lowercase!';
String_Var = raw_input ('Add, Divide, Multiply or Subtract? ');

if 'add' in String_Var:
AddVar1 = raw_input ('Number 1: ');
AddVar2 = raw_input ('Number 2: ');
Add_Var = AddVar1 + AddVar2;
print AddVar1, '+', AddVar2, '=', Add_Var;
else:

if 'subtract' in String_Var:
SubVar1 = raw_input ('Number 1: ');
SubVar2 = raw_input ('Number 2: ');
Sub_Var = SubVar1 - SubVar2;
print SubVar1, '+', SubVar2, '=', Sub_Var;
else:

if 'multiply' in String_Var:
MultiplyVar1 = raw_input ('Number 1: ');
MultiplyVar2 = raw_input ('Number 2: ');
Multpiply_Var = MultiplyVar1 + MultiplyVar2;
print MultiplyVar1, '+', MultiplyVar2, '=', Multiply_Var;
else:

if 'divide' in String_Var:
DivVar1 = raw_input ('Number 1: ');
DivVar2 = raw_input ('Number 2: ');
Div_Var = DivVar1 + DivVar2;
print DivVar1, '+', DivVar2, '=', Div_Var;
else:

print 'Syntax Error.'

I created this code, but it keeps coming up with 'else' syntax error!
Mar 13 '08 #1
Share this Question
Share on Google+
8 Replies


jlm699
100+
P: 314
Expand|Select|Wrap|Line Numbers
  1. print 'Type in lowercase!'
  2. String_Var = raw_input ('Add, Divide, Multiply or Subtract? ')
  3.  
  4. if 'add' in String_Var:
  5.     AddVar1 = raw_input ('Number 1: ')
  6.     AddVar2 = raw_input ('Number 2: ')
  7.     Add_Var = AddVar1 + AddVar2
  8.     print AddVar1, '+', AddVar2, '=', Add_Var
  9. elif 'subtract' in String_Var:
  10.     SubVar1 = raw_input ('Number 1: ')
  11.     SubVar2 = raw_input ('Number 2: ')
  12.     Sub_Var = SubVar1 - SubVar2
  13.     print SubVar1, '+', SubVar2, '=', Sub_Var
  14. elif 'multiply' in String_Var:
  15.     MultiplyVar1 = raw_input ('Number 1: ')
  16.     MultiplyVar2 = raw_input ('Number 2: ')
  17.     Multpiply_Var = MultiplyVar1 + MultiplyVar2
  18.     print MultiplyVar1, '+', MultiplyVar2, '=', Multiply_Var
  19. elif 'divide' in String_Var:
  20.     DivVar1 = raw_input ('Number 1: ')
  21.     DivVar2 = raw_input ('Number 2: ')
  22.     Div_Var = DivVar1 + DivVar2
  23.     print DivVar1, '+', DivVar2, '=', Div_Var
  24. else:
  25.     print 'Syntax Error.'
Python doesn't use semicolons or {} brackets for syntax... it's all about the indentation!
Mar 13 '08 #2

P: 5
Thank you, the else problem is solved now. What is the command for 'divide'?
Mar 13 '08 #3

jlm699
100+
P: 314
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     String_Var = raw_input ('Add, Divide, Multiply, Subtract, or Quit? ').lower()
  3.  
  4.     if 'add' in String_Var:
  5.         AddVar1 = float(raw_input ('Number 1: '))
  6.         AddVar2 = float(raw_input ('Number 2: '))
  7.         Add_Var = AddVar1 + AddVar2
  8.         print AddVar1, '+', AddVar2, '=', Add_Var
  9.     elif 'sub' in String_Var:
  10.         SubVar1 = float(raw_input ('Number 1: '))
  11.         SubVar2 = float(raw_input ('Number 2: '))
  12.         Sub_Var = SubVar1 - SubVar2
  13.         print SubVar1, '-', SubVar2, '=', Sub_Var
  14.     elif 'mul' in String_Var:
  15.         MultiplyVar1 = float(raw_input ('Number 1: '))
  16.         MultiplyVar2 = float(raw_input ('Number 2: '))
  17.         Multiply_Var = MultiplyVar1 * MultiplyVar2
  18.         print MultiplyVar1, '*', MultiplyVar2, '=', Multiply_Var
  19.     elif 'div' in String_Var:
  20.         DivVar1 = float(raw_input ('Number 1: '))
  21.         DivVar2 = float(raw_input ('Number 2: '))
  22.         Div_Var = DivVar1 / DivVar2
  23.         print DivVar1, '/', DivVar2, '=', Div_Var
  24.     elif 'q' in String_Var:
  25.         break
  26.     else:
  27.         print 'Syntax Error.'
This one's fixed for actual use of numbers... which I didn't notice before.
When you use raw_input it takes the user's input as a string. You need to type cast into either float() or int() in order to perform mathematical operations.
Mar 13 '08 #4

P: 5
Expand|Select|Wrap|Line Numbers
  1. while True:
  2.     String_Var = raw_input ('Add, Divide, Multiply, Subtract, or Quit? ').lower()
  3.  
  4.     if 'add' in String_Var:
  5.         AddVar1 = float(raw_input ('Number 1: '))
  6.         AddVar2 = float(raw_input ('Number 2: '))
  7.         Add_Var = AddVar1 + AddVar2
  8.         print AddVar1, '+', AddVar2, '=', Add_Var
  9.     elif 'sub' in String_Var:
  10.         SubVar1 = float(raw_input ('Number 1: '))
  11.         SubVar2 = float(raw_input ('Number 2: '))
  12.         Sub_Var = SubVar1 - SubVar2
  13.         print SubVar1, '-', SubVar2, '=', Sub_Var
  14.     elif 'mul' in String_Var:
  15.         MultiplyVar1 = float(raw_input ('Number 1: '))
  16.         MultiplyVar2 = float(raw_input ('Number 2: '))
  17.         Multiply_Var = MultiplyVar1 * MultiplyVar2
  18.         print MultiplyVar1, '*', MultiplyVar2, '=', Multiply_Var
  19.     elif 'div' in String_Var:
  20.         DivVar1 = float(raw_input ('Number 1: '))
  21.         DivVar2 = float(raw_input ('Number 2: '))
  22.         Div_Var = DivVar1 / DivVar2
  23.         print DivVar1, '/', DivVar2, '=', Div_Var
  24.     elif 'q' in String_Var:
  25.         break
  26.     else:
  27.         print 'Syntax Error.'
This one's fixed for actual use of numbers... which I didn't notice before.
When you use raw_input it takes the user's input as a string. You need to type cast into either float() or int() in order to perform mathematical operations.
ok thanks for your help; ive tested the program and it works perfectly now.
Mar 13 '08 #5

jlm699
100+
P: 314
Here's a little more exploration with python for ya:
Expand|Select|Wrap|Line Numbers
  1.     if 'add' in String_Var:
  2.         try:
  3.             AddVar1 = float(raw_input ('Number 1: '))
  4.             AddVar2 = float(raw_input ('Number 2: '))
  5.         except:
  6.             print 'Invalid input, numbers only please.'
  7.             continue
  8.         Add_Var = AddVar1 + AddVar2
  9.         print AddVar1, '+', AddVar2, '=', Add_Var
  10.  
Play around with this and try entering a string into the number input.
Mar 13 '08 #6

P: 5
Here's a little more exploration with python for ya:
Expand|Select|Wrap|Line Numbers
  1.     if 'add' in String_Var:
  2.         try:
  3.             AddVar1 = float(raw_input ('Number 1: '))
  4.             AddVar2 = float(raw_input ('Number 2: '))
  5.         except:
  6.             print 'Invalid input, numbers only please.'
  7.             continue
  8.         Add_Var = AddVar1 + AddVar2
  9.         print AddVar1, '+', AddVar2, '=', Add_Var
  10.  
Play around with this and try entering a string into the number input.
thanks. so, that will return an aerror message unless they enter a number?
Mar 13 '08 #7

jlm699
100+
P: 314
Yes... when using a try: except: block, you can acheive many things.
Since this is a generic except: it will catch any and all "exceptions". Sometimes it is good to have a specific exception listed, such as in this case:
Expand|Select|Wrap|Line Numbers
  1. except ValueError:
  2.  
ValueError is what is thrown when trying toconvert anything non-numerical via int() or float(). You can have multiple except statements, each with a different exception to catch different possible user-errors all followed by a blank "catch-all" except block to catch anything else that might happen. A good way to figure out what kind of error will be generated is to go into the shell, and perform something that you know will throw an exception, such as int('wtf'). ;)
There are also variations on the structure to look into (try, except, else, finally can all be incorporated). Which can provide a variety of error-checking and code balancing.
Mar 13 '08 #8

P: 5
The finished product!

Expand|Select|Wrap|Line Numbers
  1. import math
  2.  
  3. while True:
  4.     String_Var = raw_input ('Add, Divide, Multiply, Subtract, Square, Square Root, Index Notation,  or Quit? ').lower()
  5.  
  6.     if 'add' in String_Var:
  7.         try:
  8.             Var1 = float(raw_input ('Number 1: '))
  9.             Var2 = float(raw_input ('Number 2: '))
  10.         except:
  11.             print 'Invalid input, numbers only please.'
  12.             continue
  13.         Var = Var1 + Var2
  14.         print Var1, '+', Var2, '=', Var
  15.  
  16.     elif 'sub' in String_Var:
  17.         try:
  18.             Var1 = float(raw_input ('Number 1: '))
  19.             Var2 = float(raw_input ('Number 2: '))
  20.         except:
  21.             print 'Invalid input, numbers only please.'
  22.             continue
  23.         Var = Var1 - Var2
  24.         print Var1, '-', Var2, '=', Var
  25.  
  26.     elif 'div' in String_Var:
  27.         try:
  28.             Var1 = float(raw_input ('Number 1: '))
  29.             Var2 = float(raw_input ('Number 2: '))
  30.         except:
  31.             print 'Invalid input, numbers only please.'
  32.             continue
  33.         Var = Var1 / Var2
  34.         print Var1, '/', Var2, '=', Var
  35.  
  36.     elif 'mul' in String_Var:
  37.         try:
  38.             Var1 = float(raw_input ('Number 1: '))
  39.             Var2 = float(raw_input ('Number 2: '))
  40.         except:
  41.             print 'Invalid input, numbers only please.'
  42.             continue
  43.         Var = Var1 * Var2
  44.         print Var1, '*', Var2, '=', Var
  45.  
  46.     elif 'sq' in String_Var:
  47.         try:
  48.             Var1 = float(raw_input ('Number: '))
  49.         except:
  50.             print 'Invalid input, numbers only please.'
  51.             continue
  52.         Var = Var1 * Var1
  53.         print Var1, 'squared =', Var
  54.  
  55.     elif 'index' in String_Var:
  56.         try:
  57.             Var1 = float(raw_input ('Number: '))
  58.             Var2 = float(raw_input ('To the power of: '))
  59.         except:
  60.             print 'Invalid input, numbers only please.'
  61.             continue
  62.         Var = math.pow(Var1,Var2)
  63.         print Var1, 'to the power of', Var2, '=', Var
  64.  
  65.     elif 'root' in String_Var:
  66.         try:
  67.             Var1 = float(raw_input ('Number: '))
  68.         except:
  69.             print 'Invalid input, numbers only please.'
  70.             continue
  71.         Var = sqrt (Var1)
  72.         print 'the square root of', Var1, 'is', Var, '.'
  73.  
  74.  
  75.  
  76.  
  77.     elif 'q' in String_Var:
  78.         break
  79.  
  80.     else:
  81.         print 'Syntax Error.'
Mar 13 '08 #9

Post your reply

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