函数外的Python变量语法错误?

时间:2016-07-06 21:30:40

标签: python syntax

我对编程比较陌生。

我一直在尝试编写并运行我的第一个程序而且我遇到了错误。我尝试删除第22行 int(raw_input()) 几次,然后进行一些其他调整,但是我的技能并不是我自己解决的问题。

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()


返回错误

  File "try.py", line 24
Premains = (numb - Premains)
       ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:3)

我可以在这里看到两个错误。 第一个是在第20行。您已打开3个括号,只关闭其中一个。将第22行更改为:

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

不需要第三个括号,只需使用2。

第二个是在第32行和第14行。您没有将Premains传递给函数dex_skill,因此它无法访问该变量,因此它将返回TypeError。 要解决此问题,请将第32行更改为:

dex_skill(Premains)

和第14行:

def dex_skill(Premains):

所以你的代码看起来像这样:

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)

如何纠正语法错误

语法错误是最容易修复的错误之一,您将获得在Python抛出的错误中修复错误所需的所有信息。

以下代码:

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

for a in array:
    print(a)

将在第1行返回语法错误,Python IDE将突出显示无法解析的点,因此它将如下所示: Example of Syntax Error in Python 2.7

正如您所看到的,for以红色突出显示,因此Python无法解析,因此错误在该行或上面的行上。在这种情况下,数组已经打开,但没有关闭,因此Python正在解析for循环,就好像它是一个数组,它无法做到,所以你需要通过更改它来纠正第1行......

来自:array = ['john', 'paul', 'mike'

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

其他IDE应该为您提供这样的语法错误,或者在控制台上打印它,就像官方Python IDE对ValueError

所做的那样

希望这会有所帮助,DibDibs:)