Crazy weird symtax errors, please help - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Crazy weird symtax errors, please help

EmoIrish37EmoIrish37 Posts: 7Member
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 [color=Red]th[/color]en,", your_name, ".")
answer = input("""
Answer this [color=Red]th[/color]en,[color=Red]"[/color]"", your_name, """:
If two ducks have 5 ducklings, and three of those ducklings have

10 ducklings each, how many ducks do you have in total
""")
if answer == ("39"):
your_name += ("sky")
print ("Congratulations[color=Red][/color],", 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, """:
If two ducks have 5 ducklings, and three of those ducklings have

10 ducklings each, how many ducks do you have in total
""")
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!!

Comments

  • bubbatremellbubbatremell Posts: 39Member
    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
  • EmoIrish37EmoIrish37 Posts: 7Member
    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!
  • EmoIrish37EmoIrish37 Posts: 7Member
    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': """:
    If two ducks have 5 ducklings, and three of those ducklings have

    10 ducklings each, how many ducks do you have in total
    """))
    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': """:
    If two ducks have 5 ducklings, and three of those ducklings have

    10 ducklings each, how many ducks do you have in total
    """))
    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!!
  • EmoIrish37EmoIrish37 Posts: 7Member
    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!!!
  • bubbatremellbubbatremell Posts: 39Member
    answer = input("%(d)s %(e)s %(f)s" %('d': "Tell me,", 'e': nickname, 'f': """:
    If two ducks have 5 ducklings, and three of those ducklings have

    10 ducklings each, how many ducks do you have in total
    """))

    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:
    [code]
    answer = input("%(a)s %(b)s %(c)s" % {'a': "Tell me,",
    'b': your_name,
    'c': """:
    If two ducks have 5 ducklings, and three of those ducklings have

    10 ducklings each, how many ducks do you have in total
    """))[/code]

    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. :)
  • EmoIrish37EmoIrish37 Posts: 7Member
    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.
  • bubbatremellbubbatremell Posts: 39Member
    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 :)
  • EmoIrish37EmoIrish37 Posts: 7Member
    Okay...

    First off, I switched from python 3.0.1 to 2.6.4 . Seemed like a more stable version, and I thought it might help me out with these crazy errors.
    So, I went ahead and downloaded that.
    It seemed to be working better (some programs that were not working before started to) until I tested this code.
    It's coming up with more crazy syntax errors, and it's driving me up the wall!

    Okay, here's the code, and all the stuff that's supposedly a syntax error is hilighted in red:

    #Programmed by EmoIrish37, 7/30/09

    import random

    your_name = input("What is your name?")
    nicknames = ("Fruitcup", "Captain Cupcakes", "Bunny", "Kid", "Dummkopf")#Haha...
    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 %[color=Red]([/color]b)s %(c)[color=Red]s[/color]" %{'a': "Tell me,",
    'b': your_name,
    'c': """:
    If two ducks have
    ducklings, and three of those
    have ten ducklings each, how
    many ducks do you have in
    total?(Hint: ONLY the ducks
    WITH CHILDREN are
    married.)"""))
    if answer == ("40"):
    your_name += ("sky")
    print ("Congradulations,", your_name, ". You're smart!")
    answer_dos = input("%(a)s %(b)s %(c)s" %{"a" = "[color=Red]W[/color]ould [color=Red]y[/color]ou"
    "b" = " like to see"
    "c" = " the answer?"})
    if answer_dos == ("yes"):
    print """
    We have the mother and father duck, the 3 ducklings and
    their spouses, and the thirty grand-ducklings, a total of
    40 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': """:
    If two ducks have
    five ducklings, and
    three of those ducklings have
    ten ducklings each, how many
    ducks do you have in
    total? Hint: ONLY the ducks
    WITH CHILDREN are
    married.)"""))
    if answer == ("40"):
    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 thirty grand-ducklings, a total of
    40 ducks.
    """
    else:
    print ("So long, then.")
    else:
    print ("You are terrible at math.")


    Ugh, sorry, I couldn't get the indentation to work. If you need me to email the code or the file to you, that works too. My email is.. Well, we'll see if it comes to that ;)
    Thanks again!

  • EmoIrish37EmoIrish37 Posts: 7Member
    Oh, and by the way, I'm pretty sure the answer is 40.
Sign In or Register to comment.