1

I'm relatively new to programming.

I've been attempting to write and run my first program and i'm getting stuck with an error. I attempted removing the int(raw_input()) on line 22 a few times and make a few other adjustment's however my skill isn't quite all there to solve this myself.

1 Premains = 10 #Remaining points assigned before functions
2 
3 #Function designed to see user's overall stats and remaining points
4 
5 def print_skills():
6 
7         print "Dexterity:" + dex
8         print "Strength:" + str
9         print "IntelligenceL:" + int
10         print "\nYou have %d remaining" % Premain
11 
12 #Function called when user selects to edit Dexterity attribute
13 
14 def dex_skill():
15         Dpoints = 0
16         print "Great you choose Dexterity!"
17         answer = raw_input("would you like to add or subtract    points?\n > ")
18 
19         if answer == "add":
20                 print "You have: %d points remaining!" % Premains
21 
22                 numb =(int(raw_input("How many points would you like to add?\n > ")
23 
24                 Premains = (numb - Premains)
25                 Dpoints = numb + Dpoints
26 
27                 print "Great! you now have: %d Dexterity points!"
28 
29         else:
30                 print "You choose subtract."
31                 #Subtract code goes here. Similiar to above.
32 dex_skill()

~
This returns with the error

  File "try.py", line 24
Premains = (numb - Premains)
       ^
SyntaxError: invalid syntax
2
  • 3
    You are missing two close brackets in the previous line. (the outside bracket set is also uneccessary and will probably cause some weird stuff) Commented Jul 6, 2016 at 21:31
  • 1
    Just a suggestion: You will learn more and better if you try hard to figure out such things like syntax error by yourself, instead of just copy pasting things on StackOverflow Commented Jul 6, 2016 at 21:34

1 Answer 1

3

There are two errors that I can spot here. The first one is on line 20. You have opened 3 parentheses and only closed one of them. Change line 22 to this:

numb = int(raw_input("How many points would you like to add?\n > "))

There is no need for the third bracket, just use 2.

The second is on lines 32 and 14. You are not passing Premains into the function dex_skill, thereby it cannot access that variable, so it will return a TypeError. To fix this, change line 32 to this:

dex_skill(Premains)

and line 14 to this:

def dex_skill(Premains):

So your code will look like this:

Premains = 10 #Remaining points assigned before functions

#Function designed to see user's overall stats and remaining points

def print_skills():
        print "Dexterity:" + dex
        print "Strength:" + str
        print "IntelligenceL:" + int
        print "\nYou have %d remaining" % Premain

#Function called when user selects to edit Dexterity attribute
Premains = 10 #Remaining points assigned before functions

#Function designed to see user's overall stats and remaining points
def print_skills():
        print "Dexterity:" + dex
        print "Strength:" + str
        print "IntelligenceL:" + int
        print "\nYou have %d remaining" % Premain

#Function called when user selects to edit Dexterity attribute

def dex_skill(Premains): #Passing Premains as a parameter so this function can access it.
        Dpoints = 0
        print "Great you choose Dexterity!"
        answer = raw_input("would you like to add or subtract    points?\n > ")

        if answer == "add":
                print "You have: %d points remaining!" % Premains

                numb = int(raw_input("How many points would you like to add?\n > "))

                Premains = (numb - Premains)
                Dpoints = numb + Dpoints

                print "Great! you now have: %d Dexterity points!"

        else:
                print "You choose subtract."
                #Subtract code goes here. Similiar to above.

dex_skill(Premains)

HOW TO CORRECT SYNTAX ERRORS

Syntax errors are one of the easiest errors to fix, you get all of the information you need to fix it in the error thrown by Python.

The following code:

array = ['john', 'paul', 'mike'

for a in array:
    print(a)

will return a Syntax error at line 1, and the Python IDE will highlight the point where is cannot parse past, so it will look like this: Example of Syntax Error in Python 2.7

As you can see, for is highlighted in red, so Python cannot parse past that, so the error is either on that line or on the line above. In this case, the array has been opened, but not closed so Python is parsing the for loop as if it is an array, which it cannot do, so you need to correct line 1 by changing it...

From this: array = ['john', 'paul', 'mike'

To this array = ['john', 'paul', 'mike']

Other IDEs should either give you syntax errors like this or print it on the console like the official Python IDE would do for a ValueError

Hope this helps, DibDibs :)

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much! This makes total sense to me.
No problem Number28, but I agree with @pokemon, you will learn a lot more if you go through your code and look for errors. If you look back at my answer, I've added how to correct syntax errors. I'm happy to help with any problems you have, but a lot of people will just tell you to correct it yourself.
PS: Thanks for flagging me as best answer, and good luck with the rest of your project!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.