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!
8 1287 - 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
-
elif 'subtract' in String_Var:
-
SubVar1 = raw_input ('Number 1: ')
-
SubVar2 = raw_input ('Number 2: ')
-
Sub_Var = SubVar1 - SubVar2
-
print SubVar1, '+', SubVar2, '=', Sub_Var
-
elif 'multiply' in String_Var:
-
MultiplyVar1 = raw_input ('Number 1: ')
-
MultiplyVar2 = raw_input ('Number 2: ')
-
Multpiply_Var = MultiplyVar1 + MultiplyVar2
-
print MultiplyVar1, '+', MultiplyVar2, '=', Multiply_Var
-
elif '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.'
Python doesn't use semicolons or {} brackets for syntax... it's all about the indentation!
Thank you, the else problem is solved now. What is the command for 'divide'?
- 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.
- 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.
ok thanks for your help; ive tested the program and it works perfectly now.
Here's a little more exploration with python for ya: -
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.
Here's a little more exploration with python for ya: -
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?
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:
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.
The finished product! - 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.'
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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 =
|
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
|
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).
|
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.
| |
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) {
|
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()
|
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 *
|
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...
|
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);}
|
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...
| |
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...
|
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...
|
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,...
|
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...
|
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...
|
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();...
| |
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |