2016-07-06 26 views
1

我对编程比较陌生。函数外部的Python变量语法错误?

我一直在试图编写和运行我的第一个程序,我陷入了一个错误。我尝试了几次删除第22行 int(raw_input())中的10然而做了一些其他的调整,但我的技能并不是完全在那里解决了这个问题。

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 
+3

您在上一行缺少两个紧邻括号。 (外括号集也是不必要的,可能会导致一些奇怪的东西) –

+1

只是一个建议:如果你努力弄清楚像你自己的语法错误这样的事情,而不是在StackOverflow上复制粘贴事物,你会学到更多,更好的东西 – pokemon

回答

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

+1

非常感谢!这对我来说是完全意义上的。 – Number28

+0

没问题Number28,但我同意@pokemon,如果你仔细查看代码并寻找错误,你将学到更多东西。如果你回头看我的答案,我已经添加了如何纠正语法错误。 我很乐意为您解决任何问题提供帮助,但很多人会告诉您自己改正。 – DibDibs

+0

PS:感谢您将我标记为最佳答案,并祝您的项目其他成果顺利! – DibDibs