[Tutor] Won't run. Syntax Error.

Alan Gauld alan.gauld at btinternet.com
Mon Sep 9 01:16:28 CEST 2013


On 08/09/13 20:43, Kimberly Mansfield wrote:
> This is the first homework assignment. Driving me crazy - struggling
> with it for two entire days. I want to drop this class now.
>

Hi, welcome to tutor.

Can you explain exactly how you are running the program because it looks 
below like there are twi distinct things going on.

> I think it is all entered correctly, but it won't run and teacher will
> not accept it if it won't run. Says "syntax error" and highlights one of
> the numbers in the top line that was there when I first opened it,
> before I typed anything. Specifically, this:
> Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32
> bit (Intel)] on win32
> The middle 3 (in red) is highlighted. There is no arrow pointing to it
> or anything else.

This looks like you have copied a Python interpreter interactive session 
into a text file and tried to run it as a script. Is that what you did?

If so, that's the problem. You need to separate out what the interpreter 
displayed (its output) from what the interpreter read(its input). The 
interpreters input is all you should have in your program.

> month. The teacher gave us an IPO chart with these definitions and we
> are supposed to write the code for it in Python.

Wow, that's quite an advanced first class! I didn't see IPO diagrams 
till my third year at Uni! And then never used them again until I 
started using 6 Sigma as a business analyst at work many years
later... (And even then usually in the context of SIPOC charts...)

> Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32
> bit (Intel)] on win32
> Type "copyright", "credits" or "license()" for more information.
>  >>> Pizzas = float (input ("Number of Pizzas "))
> Number of Pizzas 100
>  >>> Length = float (input ("Length of Pizzas "))
> Length of Pizzas 11
>  >>> Width = float (input ("Width of Pizzas "))
> Width of Pizzas 11
>  >>> Thickness = float (input ("Thickness of Pepperoni Slices "))
> Thickness of Pepperoni Slices .1
>  >>> Diameter = float (input ("Diameter of Pepperoni Slices "))
> Diameter of Pepperoni Slices 1
>  >>> Edge = float (input ("Crust Edge of Pizzas "))
> Crust Edge of Pizzas .5
>  >>> StickLength = float (input ("Length of Pepperoni Sticks "))
> Length of Pepperoni Sticks 10
>  >>> UsefulLength = Length - 2 * Edge
>  >>> SlicesLength = UsefulLength / Diameter
>  >>> UsefulWidth = Width - 2 * Edge
>  >>> SlicesWidth = UsefulWidth / Diameter
>  >>> Slices = SlicesWidth * SlicesLength
>  >>> TotalSlices = Slices * Pizzas
>  >>> HypoLength = TotalSlices * Thickness
>  >>> PepperoniSticks = HypoLength / StickLength
>  >>> print (StickLength)
> 10.0
>  >>> print ("You will need", PepperoniSticks, "pepperoni sticks this
> month.")
> You will need 100.0 pepperoni sticks this month.

Down to here it looks like a perfectly successful interactive session.
Converting that to a script that you can run directly it looks like:

Pizzas = float (input ("Number of Pizzas "))
Length = float (input ("Length of Pizzas "))
Width = float (input ("Width of Pizzas "))
Thickness = float (input ("Thickness of Pepperoni Slices "))
Diameter = float (input ("Diameter of Pepperoni Slices "))
Edge = float (input ("Crust Edge of Pizzas "))
StickLength = float (input ("Length of Pepperoni Sticks "))
UsefulLength = Length - 2 * Edge
SlicesLength = UsefulLength / Diameter
UsefulWidth = Width - 2 * Edge
SlicesWidth = UsefulWidth / Diameter
Slices = SlicesWidth * SlicesLength
TotalSlices = Slices * Pizzas
HypoLength = TotalSlices * Thickness
PepperoniSticks = HypoLength / StickLength
print (StickLength)
print ("You will need", PepperoniSticks, "pepperoni sticks this month.")

And the output when you run it is:

Number of Pizzas 100
Length of Pizzas 11
Width of Pizzas 11
Thickness of Pepperoni Slices .1
Diameter of Pepperoni Slices 1
Crust Edge of Pizzas .5
Length of Pepperoni Sticks 10
10.0
You will need 100.0 pepperoni sticks this month.

So it looks like it all works. We could critique the
style and design but the basic code works.

I suspect your probl;em is that you are trying to save
or cut 'n paste an interpreter session directly into
a python script and that just won't work.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list