Got something to write about? Check out our Article Builder.

Python

Moderators: None (Apply to moderate this forum)
Number of threads: 297
Number of posts: 898

This Forum Only
Post New Thread

Report
Crazy weird symtax errors, please help Posted by EmoIrish37 on 9 Nov 2009 at 2:02 PM
Hey, I just got the newest version of python, Python 3.1.
I am getting the craziest, most out-there syntax errors ever, and I have no idea what to do.
Okay, here's a section of code that i just wrote to prove how crazy these errors really are:

import random

your_name = input("What is your name?")
nicknames = ("Fruitcup", "Captain", "Bunny", "Mama", "Dummkopf")
nickname = random.choice(nicknames)
or_not = input("Do you mind if I call you", nickname, "?")
if or_not == ("yes"):
print ("Fine then,", your_name, ".")
answer = input("""
Answer this then,""", your_name, """:
\tIf two ducks have 5 ducklings, and three of those ducklings have\n
\t10 ducklings each, how many ducks do you have in total\n""")
if answer == ("39"):
your_name += ("sky")
print ("Congratulations,", your_name, ". You're smart!")
answer_dos = input("Would you like to see the solution?")
if answer_dos == ("yes"):
print """
We have the mother and father duck, the three ducklings and their
spouses, and the thirty grand-ducklings, a total of 39 ducks.
"""
else:
print ("So long then!")
else:
print ("You are terrible at math.")


okay, all the things hi-lighted in red are the things that python is saying is a syntax error. the indenting is not right because i copied and pasted it, but you guys get the general idea:

import random

your_name = input("What is your name?")
nicknames = ("Fruitcup", "Captain", "Bunny", "Mama", "Dummkopf")
nickname = random.choice(nicknames)
or_not = input("Do you mind if I call you", nickname, "?")
if or_not == ("yes"):
print ("Fine then,", your_name, ".")
answer = input("""
Answer this then,""", your_name, """:
\tIf two ducks have 5 ducklings, and three of those ducklings have\n
\t10 ducklings each, how many ducks do you have in total\n""")
if answer == ("39"):
your_name += ("sky")
print ("Congratulations,", your_name, ". You're smart!")
answer_dos = input("Would you like to see the solution?")
if answer_dos == ("yes"):
print """
We have the mother and father duck, the three ducklings and their
spouses, and the thirty grand-ducklings, a total of 39 ducks.
"""
else:
print ("So long then!")
else:
print ("You are terrible at math.")

Like, what is up with that!!?!??!?!?!

Okay, I'm pretty new to programming in general. I was a script kiddie with batch files and .vbs for a while, until i decided i wanted to do something besides making lame joke viruses and putting them on my sister's computer. I've had lots of problems so far, but i've been able to work through them all. Except for this. I've put hours into trying to fix this and nothing works. Please, i just need a little help. Even if you only know what not to do, but not nescesarily how to fix the problem, then by all means comment. any and all comments are appreciated. thanks!!
Report
Re: Crazy weird symtax errors, please help Posted by bubbatremell on 10 Nov 2009 at 3:16 PM
I'm not sure why it's coming up as a syntax error w/o the indentation intact, but I can tell you that the input command is only designed to take one arg. If you really wanna do some fancy string work, I recommend using %s as follows:

or_not = input("Do you mind if I call you %s?" % nickname)

%s lets you insert stuff into strings. Your input command is interpreting input("stuff", variable, "moar stuff") as three args. Using %s, you can jam it all into one, input-function-compatible string. If you need multiple insertions, the stuff following the % must be a tuple:
>>> print("%s, %s, %s" % (1, 2, 3))
1, 2, 3

Also, if you want, you can name your string args:
>>> print("%(s)s %(v)s %(o)s" % {'s': 'dogs', 'v': 'eat', 'o': 'birds'})
dogs eat birds

The % does some other stuff, too, but I only ever use it with 's'.

Also, the answer is 40 b/c the first ducks had 5 ducklings, not 3.
:P
Report
Re: Crazy weird symtax errors, please help Posted by EmoIrish37 on 10 Nov 2009 at 6:39 PM
Okay, thanks for clearing up my math errors and telling me about %s.
That was reaaaaally helpful!
ANd I feel stupid now xD
Thanks again!
Report
Re: Crazy weird symtax errors, please help Posted by EmoIrish37 on 10 Nov 2009 at 7:23 PM
Okay, I reformatted it according to bubbatremell's suggestions (Thanks again!) and it is STILL doing crazy stuff:

import random

your_name = input("What is your name?")
nicknames = ("Fruitcup", "Captain", "Bunny", "Mama", "Dummkopf")
nickname = random.choice(nicknames)
or_not = input("Do you mind if I call you %s?" % nickname)
if or_not == ("yes"):
print ("Fine then,", your_name, ".")
answer = input("%(a)s %(b)s %(c)s" %{'a': "Tell me,", 'b': your_name, 'c': """:
\tIf two ducks have 5 ducklings, and three of those ducklings have\n
\t10 ducklings each, how many ducks do you have in total\n"""))
if answer == ("50"):
your_name += ("sky")
print ("Congradulations,", your_name, ". You're smart!")
answer_dos = input("Would you like to see the solution?")
if answer_dos == ("yes"):
print """
We have the mother and father duck, the 5 ducklings and their
spouses, and the thirty grand-ducklings, a total of 50 ducks.
"""
else:
print ("So long then!")
else:
print ("You are terrible at math. Goodbye!")
input ("Please press the enter key to exit.")
else:
print ("HORRAY!")
answer = input("%(d)s %(e)s %(f)s" %('d': "Tell me,", 'e': nickname, 'f': """:
\tIf two ducks have 5 ducklings, and three of those ducklings have\n
\t10 ducklings each, how many ducks do you have in total\n"""))
if answer == ("50"):
nickname = your_name += ("sky")
print ("Congradulations,", nickname, ". You're smart!")
answer_dos = input("Would you like to see the solution?")
if answer_dos == ("yes"):
print """
We have the mother and father duck, the 5 ducklings and their
spouses, and the thirty grand-ducklings, a total of 50 ducks.
"""
else:
print ("So long, then.")
else:
print ("You are terrible at math.")


All the stuff hi-lighted in red is stuff it's saying is a syntax error.
I mean, COME ON!!
Does anyone think that it might be something wrong with my version of python? Like, my comp screwed up the encoding or something?
Please get back to me on this!!
Report
Re: Crazy weird symtax errors, please help Posted by EmoIrish37 on 10 Nov 2009 at 7:26 PM
snap, I forgot to hilight.
Well, the syntax error were stuff like the word "you", the character %, and the quote marks ("").
Outrageous stuff.
I mean, seriously, something is up with my comp, and I have NO IDEA what it is!!!
Report
Re: Crazy weird symtax errors, please help Posted by bubbatremell on 11 Nov 2009 at 11:47 AM
answer = input("%(d)s %(e)s %(f)s" %('d': "Tell me,", 'e': nickname, 'f': """:
\tIf two ducks have 5 ducklings, and three of those ducklings have\n
\t10 ducklings each, how many ducks do you have in total\n"""))

You declared the args to % as a tuple, and not as a dict. You want a tuple for multiple unnamed args, and a dict for any number of named args.

Also, it tends to help readability if you write out lists of things as such:
answer = input("%(a)s %(b)s %(c)s" % {'a': "Tell me,",
                                      'b': your_name,
                                      'c': """:
\tIf two ducks have 5 ducklings, and three of those ducklings have\n
\t10 ducklings each, how many ducks do you have in total\n"""))


Or something like that. PEP8 insists that you limit your lines to 80 characters whenever possible, so breaking lists or arguments out to separate lines == pretty.

I find that if I have a syntax error somewhere, it usually shows up somewhere besides where the actual error is. This has to do with how Python is parsing your code. It thinks that whatever you wrote is legitimate, but as something else.

Also, the answer is 40. :)
Report
Re: Crazy weird symtax errors, please help Posted by EmoIrish37 on 16 Nov 2009 at 7:53 PM
Okay, this computer is just crazy. it's official.
My version of Python is officially considered messed up.
It keeps saying I have a syntax error, but it won't tell me what it is!!!
A window saying 'syntax error' pops up, but nothing is hi-lighted, and nothing looks out of place!!!
Come on!!!
now, you cannot say that Python is interpreting THAT wrong.
Report
Re: Crazy weird symtax errors, please help Posted by bubbatremell on 18 Nov 2009 at 11:34 AM
I'll take another look if you wanna paste code w/ all indentation and what version of python you are using. It can't be debugged w/o indentation. Don't worry if it doesn't show up quite right on the page-- I can always hit view page source. As long as it's there :)



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A bootstrapLabs project.