444,050 Members | 1,009 Online 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
8 Replies

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

 100+ P: 314 Expand|Select|Wrap|Line Numbers while True:     String_Var = raw_input ('Add, Divide, Multiply, Subtract, or Quit? ').lower()       if 'add' in String_Var:         AddVar1 = float(raw_input ('Number 1: '))         AddVar2 = float(raw_input ('Number 2: '))         Add_Var = AddVar1 + AddVar2         print AddVar1, '+', AddVar2, '=', Add_Var     elif 'sub' in String_Var:         SubVar1 = float(raw_input ('Number 1: '))         SubVar2 = float(raw_input ('Number 2: '))         Sub_Var = SubVar1 - SubVar2         print SubVar1, '-', SubVar2, '=', Sub_Var     elif 'mul' in String_Var:         MultiplyVar1 = float(raw_input ('Number 1: '))         MultiplyVar2 = float(raw_input ('Number 2: '))         Multiply_Var = MultiplyVar1 * MultiplyVar2         print MultiplyVar1, '*', MultiplyVar2, '=', Multiply_Var     elif 'div' in String_Var:         DivVar1 = float(raw_input ('Number 1: '))         DivVar2 = float(raw_input ('Number 2: '))         Div_Var = DivVar1 / DivVar2         print DivVar1, '/', DivVar2, '=', Div_Var     elif 'q' in String_Var:         break     else:         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

 100+ P: 314 Here's a little more exploration with python for ya: Expand|Select|Wrap|Line Numbers     if 'add' in String_Var:         try:             AddVar1 = float(raw_input ('Number 1: '))             AddVar2 = float(raw_input ('Number 2: '))         except:             print 'Invalid input, numbers only please.'             continue         Add_Var = AddVar1 + AddVar2         print AddVar1, '+', AddVar2, '=', Add_Var   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     if 'add' in String_Var:         try:             AddVar1 = float(raw_input ('Number 1: '))             AddVar2 = float(raw_input ('Number 2: '))         except:             print 'Invalid input, numbers only please.'             continue         Add_Var = AddVar1 + AddVar2         print AddVar1, '+', AddVar2, '=', Add_Var   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

 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 except ValueError:   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 import math   while True:     String_Var = raw_input ('Add, Divide, Multiply, Subtract, Square, Square Root, Index Notation,  or Quit? ').lower()       if 'add' in String_Var:         try:             Var1 = float(raw_input ('Number 1: '))             Var2 = float(raw_input ('Number 2: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = Var1 + Var2         print Var1, '+', Var2, '=', Var       elif 'sub' in String_Var:         try:             Var1 = float(raw_input ('Number 1: '))             Var2 = float(raw_input ('Number 2: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = Var1 - Var2         print Var1, '-', Var2, '=', Var       elif 'div' in String_Var:         try:             Var1 = float(raw_input ('Number 1: '))             Var2 = float(raw_input ('Number 2: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = Var1 / Var2         print Var1, '/', Var2, '=', Var       elif 'mul' in String_Var:         try:             Var1 = float(raw_input ('Number 1: '))             Var2 = float(raw_input ('Number 2: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = Var1 * Var2         print Var1, '*', Var2, '=', Var       elif 'sq' in String_Var:         try:             Var1 = float(raw_input ('Number: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = Var1 * Var1         print Var1, 'squared =', Var       elif 'index' in String_Var:         try:             Var1 = float(raw_input ('Number: '))             Var2 = float(raw_input ('To the power of: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = math.pow(Var1,Var2)         print Var1, 'to the power of', Var2, '=', Var       elif 'root' in String_Var:         try:             Var1 = float(raw_input ('Number: '))         except:             print 'Invalid input, numbers only please.'             continue         Var = sqrt (Var1)         print 'the square root of', Var1, 'is', Var, '.'             elif 'q' in String_Var:         break       else:         print 'Syntax Error.' Mar 13 '08 #9 