PLEASE HELP! :) SYNTAX error problem

A forum for general discussion of the Python programming language.

PLEASE HELP! :) SYNTAX error problem

Postby pimaster » Tue Sep 24, 2013 4:25 pm

Hi please help!
I have a computing homework due in for tomorrow and I am really confused why it doesn't work! It is very simple, I hear, and so should be no problem for you to fix, if you would be so kind :)
The project is, that the user inputs a 4 digit decimal number and then answers a menu on how they would like it to be outputted - either a word place value or number place value. I am using strings to split the number, and have the code nearly ready, I think, however I keep getting syntax errors and am coming close to punching my computer! Here is the code:


Code: Select all
string=str(int(input("Enter a number <10000 ")))

num1=int(string[0])
num2=int(string[1])
num3=int(string[2])
num4=int(string[3])

number1=num1*1
number2=num2*10
number3=num3*100
number4=num4*1000

answer1=("The number has: \n\
",num4,"thousands \n\
",num3,"hundeds \n\
",num2,"tens \n\
",num1,"units")

answer2=("Place value: \n\
",num4,"     ",number4" \n\
",num3,"     ",number3" \n\
",num2,"     ",number2" \n\
",num1,"     ",number1" \n)

menuanswer=int(input("Would you like to see: \n\
1. Word place value \n\
2. Number place value \n\
3. Both word & number place value")

if menuanswer == 1:
    print(answer1)

elif menuanswer == 2:
    print(answer2)

elif menuanswer == 3:
    print(answer1,answer2)

else:
    print("You have not inputted correctly")


Please help me, it would mean a lot :) And thank you for any help you choose to give!
Last edited by micseydel on Tue Sep 24, 2013 4:45 pm, edited 1 time in total.
Reason: First post lock.
pimaster
 
Posts: 3
Joined: Tue Sep 24, 2013 4:17 pm

Re: PLEASE HELP! :) SYNTAX error problem

Postby micseydel » Tue Sep 24, 2013 4:44 pm

On lines 19-23, you missed commas and a close quote. On line 28 you forgot a close paren.

When writing code, you should test incrementally. That is, right some code, test it. Don't right a bunch of code before testing it because then it becomes very difficult to find bugs.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1116
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: PLEASE HELP! :) SYNTAX error problem

Postby pimaster » Tue Sep 24, 2013 4:51 pm

Thanks a lot! The errors have been rectified, other than the one you pointed out in line 28, not sure what a 'close paren' is.. please help :)
pimaster
 
Posts: 3
Joined: Tue Sep 24, 2013 4:17 pm

Re: PLEASE HELP! :) SYNTAX error problem

Postby hrs » Tue Sep 24, 2013 5:08 pm

It's a round bracket like this: )

You should always provide a traceback if you get one.
hrs
 
Posts: 86
Joined: Thu Feb 07, 2013 9:26 pm

Re: PLEASE HELP! :) SYNTAX error problem

Postby micseydel » Tue Sep 24, 2013 5:17 pm

"Paren" is short for "parenthesis".

hrs wrote:You should always provide a traceback if you get one.

Often times they're using an IDE which just says "syntax error" and doesn't provide more detail. If that is the case, pimaster, do note that if you run your script on the command line, Python will point to where in the file it doesn't understand your code and gets a SyntaxError. For at least one of the bits of code you had here, the error appeared on a later line than the problem occurred, when paren was missed. This is because a close paren problem is detected once it hits something that should come only after a close paren. So the problem on line 28 was indicated by an error on line 30. It'll be second nature eventually, to look on earlier lines when you see a strange SyntaxError.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1116
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: PLEASE HELP! :) SYNTAX error problem

Postby metulburr » Tue Sep 24, 2013 5:39 pm

Often times they're using an IDE which just says "syntax error" and doesn't provide more detail.

Interesting. I wasn't aware that some IDE's did not give out the full traceback.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1312
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: PLEASE HELP! :) SYNTAX error problem

Postby micseydel » Tue Sep 24, 2013 6:04 pm

I just tried to create and screenshot an example from IDLE, but it provided more data than I expected for my examples. When I tried the OP's code though, it indicates a problem at the end of line 22 when it should point to line 20 first. It's still not very helpful. Even the SyntaxError from the Python interpreter says line 21 here.

pimaster: really, that's terrible code that's prone to error and unhelpful feedback from the interpreter. Nothing good comes from escaping the newline to just put a close quote on the next line. Your first tuple can be written this way
Code: Select all
answer1 = ("The number has: \n",
           num4, "thousands \n",
           num3, "hundeds \n",
           num2, "tens \n",
           num1, "units")

I'm not sure why you're doing that anyway though since it's a tuple and you're not forming a string out of all that.
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1116
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA


Return to General Discussions

Who is online

Users browsing this forum: No registered users and 2 guests