Intermediate Python user needed help

  • Thread starter John Mordecai Dildy
  • Start date
J

John Mordecai Dildy

I am currently using python 2.6 and am not going to install the newer versions of python and i am looking for people that are still using ver 2.6 in python to help with with the code line:

sentence = "All good things come to those who wait."

then im getting this error message when i dont see the problem with it:

File "ex26.py", line 77
sentence = "All good things come to those who wait."
^
SyntaxError: invalid syntax

Please help me

Email me at (e-mail address removed) if you have any help for me
 
J

John Ladasky

Check line 76 of your code for errors.

If line 76 is incorrectly formed, Python may see line 77 as a continuation of line 76 and throw the SyntaxError because of that.
 
J

John Mordecai Dildy

Well 75 and 76 is a blank line of text but i will see if i can take out those lines to see if it is the problem thanks John
 
J

John Mordecai Dildy

Ive tried to delete the spaces in 75 and 76 to see if it made a change but it has not made a difference to it. Here is the full code and the thing isi know there is things wrong with it but the thing is im fixing a code fora friend to help him getting with the coding:


def break_words(stuff):
"""This function will break up words for us."""
words = stuff.split(' ')
return words

def sort_words(words):
"""Sorts the words."""
return sorted(words)

def print_first_word(words):
"""Prints the first word after popping it off."""
word = words.pop(0)
print word

def print_last_word(words):
"""Prints the last word after popping it off."""
word = words.pop(-1)
print word

def sort_sentence(sentence):
"""Takes in a full sentence and returns the sorted words."""
words = break_words(sentence)
return sort_words(words)

def print_first_and_last(sentence):
"""Prints the first and last words of the sentence."""
words = break_words(sentence)
print_first_word(words)
print_last_word(words)

def print_first_and_last_sorted(sentence):
"""Sorts the words then prints the first and last one."""
words = sort_sentence(sentence)
print_first_word(words)
print_last_word(words)


print "Let's practice everything."
print 'You\'d need to know \'bout escapes with \\ that do \n newlines and \t tabs.'

poem = """
\tThe lovely world
with logic so firmly planted
cannot discern \n the needs of love
nor comprehend passion from intuition
and requires an explantion
\n\t\twhere there is none.
"""


print "--------------"
print poem
print "--------------"

five = 10 - 2 + 3 - 5
print "This should be five: %s" % five

def secret_formula(started):
jelly_beans = started * 500
jars = jelly_beans / 1000
crates = jars / 100
return jelly_beans, jars, crates


start_point = 10000
beans, jars, crates == secret_formula(start-point)

print "With a starting point of: %d" % start_point
print "We'd have %d jeans, %d jars, and %d crates." % (beans, jars, crates)

start_point = start_point / 10

print "We can also do that this way:"
print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
sentence = "All good things come to those who wait."

words = ex25.break_words(sentence)
sorted_words = ex25.sort_words(words)

print_first_word(words)
print_last_word(words)
..print_first_word(sorted_words)
print_last_word(sorted_words)
sorted_words = ex25.sort_sentence(sentence)
prin sorted_words

print_irst_and_last(sentence)

print_first_a_last_sorted(senence)


Thank you in advance to anyone that can help me with this code
 
A

Andrew Berg

print "We'd have %d beans, %d jars, and %d crabapples." % secret_formula(start_pont
sentence = "All good things come to those who wait."
You are missing a parenthesis at the end of the previous line.
.print_first_word(sorted_words)
That dot will make this line raise an error.

You should use an IDE or something to highlight syntax errors.
 
J

John Mordecai Dildy

well that work on mac though?
im asking because i see the Windows NT at the bottom of your reply and plus im using 2.6 python not 3.3
 
J

John Mordecai Dildy

well that work on mac though?

im asking because i see the Windows NT at the bottom of your reply and plus im using 2.6 python not 3.3

i see the ) problem i have it fixed
 
J

John Mordecai Dildy

File "ex26.py", line 84
.print_first_word(sorted_words)
^
SyntaxError: invalid syntax

is what i have now and i dont see the problem like usual (i only post problems that i cant fix).
 
S

Steven D'Aprano

Ive tried to delete the spaces in 75 and 76 to see if it made a change
but it has not made a difference to it.

What made you think that the problem could be fixed by deleting *spaces*?

In general, making random changes to code in the hope that syntax errors
will just go away is not the right way to fix broken code. Even if you
succeed, since you don't know what you did to fix it, how do you know
that your next change won't break it again?

Almost always, when you have a mysterious syntax error on a line that
appears to be perfectly fine, the reason is a missing bracket of some
sort on a previous line. (For Americans, I mean parentheses, brackets or
braces; for Britons and Australians, round square or curly brackets; for
everyone else, whatever you call them.)
 
J

John Mordecai Dildy

What made you think that the problem could be fixed by deleting *spaces*?



In general, making random changes to code in the hope that syntax errors

will just go away is not the right way to fix broken code. Even if you

succeed, since you don't know what you did to fix it, how do you know

that your next change won't break it again?



Almost always, when you have a mysterious syntax error on a line that

appears to be perfectly fine, the reason is a missing bracket of some

sort on a previous line. (For Americans, I mean parentheses, brackets or

braces; for Britons and Australians, round square or curly brackets; for

everyone else, whatever you call them.)

Well i have put the spaces back into the code after i did that and had no hope it worked though.

thank you steven for giving me some input
 
J

John Mordecai Dildy

Current Problem at the moment

Traceback (most recent call last):
File "ex26.py", line 66, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined

anyone know how to make start defined
 
R

Roy Smith

John Mordecai Dildy said:
Current Problem at the moment

Traceback (most recent call last):
File "ex26.py", line 66, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined

anyone know how to make start defined

You gotta give us more to go on than that. Adding a line:

start = 0

right before line 66 will make it defined, but it's almost certainly not
what you want. Give us a little context. What value did you expect
start would have?
 
T

Tim Chase

Current Problem at the moment

Traceback (most recent call last):
File "ex26.py", line 66, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined

anyone know how to make start defined

"start-point" is not a valid identifier as the "-" isn't permitted
in a variable name. This is the case for just about every language
out there. HTML/XML & CSS are the only languages that come to my
mind in which the dash is considered a valid part of an identifier.

You either mean something like "start_point" (with an underscore
instead of a minus), or you're performing a subtraction of "start
minus point", in which case you'd have to assign those values before
you use them.

-tkc
 
V

Vlastimil Brom

2012/8/5 John Mordecai Dildy said:
Current Problem at the moment

Traceback (most recent call last):
File "ex26.py", line 66, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined

anyone know how to make start defined

Hi,
you would need to assign something to the name "start", e.g.
start = 3
the next error would probably be undefined "point", which should get
subtracted from "start" according to the code ...

However, in your case, this is likely another typo in the code,
you probably want "start_point", which is defined in the script already.

hth,
vbr
 
Z

Zero Piraeus

:

Current Problem at the moment

Traceback (most recent call last):
File "ex26.py", line 66, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined

anyone know how to make start defined

I think you could help yourself by reading a good tutorial or two ...
so far, the problems you've raised suggest quite strongly that you
either aren't reading the code with anything like the care that you
need to, or just don't understand Python at all.

Hint: if the offending line were written like this, would it be more
obvious what's wrong?

beans, jars, crates = secret_formula(start - point)

-[]z.
 
M

MRAB

Current Problem at the moment

Traceback (most recent call last):
File "ex26.py", line 66, in <module>
beans, jars, crates = secret_formula(start-point)
NameError: name 'start' is not defined

anyone know how to make start defined
You have "start-point", but I think that should be "start_point",
judging by the previous line.
 
J

John Mordecai Dildy

Thanks everyone that has put input into this its working on out error by error
 
M

MRAB

"start-point" is not a valid identifier as the "-" isn't permitted
in a variable name. This is the case for just about every language
out there. HTML/XML & CSS are the only languages that come to my
mind in which the dash is considered a valid part of an identifier.
I believe that Cobol allows "-" in names.
 
J

John Mordecai Dildy

Im using Textwrangler and thats the only text editor that im using just saying for everyone
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top