473,809 Members | 2,781 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Else error

5 New Member
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 1287
jlm699
314 Contributor
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
callumagus
5 New Member
Thank you, the else problem is solved now. What is the command for 'divide'?
Mar 13 '08 #3
jlm699
314 Contributor
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
callumagus
5 New Member
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
314 Contributor
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
callumagus
5 New Member
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
314 Contributor
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
callumagus
5 New Member
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

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

Similar topics

11
97210
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I have written a theme in python for the superkaramba theme engine on kde (see http://netdragon.sourceforge.net - if you are a kde/linux user, it is a great visual applet engine). I have uploaded it to www.kdelook.org for others to download and...
39
6081
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text containing norwegian special characters æ, ø and å. >>> liste =
33
3872
by: Diez B. Roggisch | last post by:
Hi, today I rummaged through the language spec to see whats in the for ... else: for me. I was sort of disappointed to learn that the else clauses simply gets executed after the loop-body - regardless of the loop beeing entered or not. So where is an actual use case for that feature? I imagined that the else-clause would only be executed if the loop body
3
3938
by: Amy | last post by:
Hi, I have 6 If Then Else statements I was supposed to write. I did so but I know that they have to be wrong because they all look the same. Could someone take a look at them and point me in the right direction about what I am not doing correctly? 1.. Write an If Then Else statement that displays the string "Pontiac" in the CarMakeLabel control if the CarTextBox control contains the string "Grand Am" (in any case).
32
6133
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I understand Finally runs whether an error was caught or not, I haven't found a use for finally yet.
8
2817
by: Jim Michaels | last post by:
C:\prj\quiz\withusers>php tareports.php PHP Parse error: syntax error, unexpected T_ELSE in C:\prj\quiz\withusers\tareports.php on line 205 this is the section of code. if (isset($row4)) { if (isset($row5)) { //answer given? if ($row4==$row5) {
20
3933
by: John Salerno | last post by:
I'm starting out with this: try: if int(text) 0: return True else: self.error_message() return False except ValueError: self.error_message()
2
4104
by: juan-manuel.behrendt | last post by:
Hello together, I wrote a script for the engineering software abaqus/CAE. It worked well until I implemented a selection in order to variate the variable "lGwU" through an if elif, else statement. I am going to post the first 82 lines of the script, since the error message points at line 80: from abaqusConstants import * from abaqus import *
8
18773
by: pelicanstuff | last post by:
Hi - Was wondering if anybody could tell me why this rather crappy code is giving me an 'Else without If' error on compile? All the Elses and Ifs look ok to me but there's a few. Private Sub MySearchButtonClick() If gcfHandleErrors Then On Error GoTo PROC_ERR PushCallStack "MySearchButton" If Me.AllowFlexibility = True Then If Me.UseSpecialism = True And Me.UseCountryExperience = True Then If Me.Active = True...
11
2099
by: Chad | last post by:
The question stems from some code at the following url http://www.cplusplus.com/reference/clibrary/cstdio/fread.html In the code example they have a single if statement for the following pFile = fopen ( "myfile.bin" , "rb" ); if (pFile==NULL) {fputs ("File error",stderr); exit (1);}
0
9721
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
10639
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
10376
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
10383
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,...
0
10120
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7661
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
6881
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();...
1
4332
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3861
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.